我在我的
Android应用中使用谷歌地图.我需要将地图重新定位到客户端的当前位置.我使用了以下声明 –
map.setmylocationenabled(true);
这会在右上方显示一个按钮,但单击该按钮不起作用.
按钮单击侦听器:
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { @Override public boolean onMyLocationButtonClick() { mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng,zoomLevel)); return false; } });
解决方法
只需从
my other answer here获取代码,然后修改按钮单击侦听器以请求其他位置:
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { @Override public boolean onMyLocationButtonClick() { if (mGoogleApiClient != null) { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this); } return false; } });
然后onLocationChanged()中的代码将重新定位摄像机位置,然后再次注册位置更新:
@Override public void onLocationChanged(Location location) { mLastLocation = location; if (mCurrLocationMarker != null) { mCurrLocationMarker.remove(); } //Place current location marker LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude()); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title("Current Position"); markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); mCurrLocationMarker = mGoogleMap.addMarker(markerOptions); //move map camera mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11)); if (mGoogleApiClient != null) { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this); } }