我的谷歌地图api有问题。我想要做的是将相机与位置一起移动,使蓝色指示灯始终位于中心位置。
认为放置代码的好地方在 OnLocationChanged方法,但事实并非如此。
我尝试在那里运行此代码:
LatLng latlng = new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latlng,17);
mMap.animateCamera(cameraUpdate);
更新
那么我不能这样做呢?@Override
public void onLocationChanged(Location location) {
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng)
.zoom(ZOOM)
.bearing(0)
.tilt(0)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
答案 0 :(得分:1)
该代码可用于更新您的相机位置,但不会精确居中Google地图蓝色指示器。这是因为蓝点正在侦听与您在代码片段中创建的不同的locationRequest(它似乎没有做任何事情)。以下代码段来自here
/* Our custom LocationSource.
* We register this class to receive location updates from the Location Manager
* and for that reason we need to also implement the LocationListener interface. */
private class FollowMeLocationSource implements LocationSource, LocationListener {
private OnLocationChangedListener mListener;
private LocationManager locationManager;
private final Criteria criteria = new Criteria();
private String bestAvailableProvider;
/* Updates are restricted to one every 10 seconds, and only when
* movement of more than 10 meters has been detected.*/
private final int minTime = 10000; // minimum time interval between location updates, in milliseconds
private final int minDistance = 10; // minimum distance between location updates, in meters
private FollowMeLocationSource() {
// Get reference to Location Manager
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
// Specify Location Provider criteria
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(true);
criteria.setBearingRequired(true);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(true);
}
private void getBestAvailableProvider() {
/* The preferred way of specifying the location provider (e.g. GPS, NETWORK) to use
* is to ask the Location Manager for the one that best satisfies our criteria.
* By passing the 'true' boolean we ask for the best available (enabled) provider. */
bestAvailableProvider = locationManager.getBestProvider(criteria, true);
}
/* Activates this provider. This provider will notify the supplied listener
* periodically, until you call deactivate().
* This method is automatically invoked by enabling my-location layer. */
@Override
public void activate(OnLocationChangedListener listener) {
// We need to keep a reference to my-location layer's listener so we can push forward
// location updates to it when we receive them from Location Manager.
mListener = listener;
// Request location updates from Location Manager
if (bestAvailableProvider != null) {
locationManager.requestLocationUpdates(bestAvailableProvider, minTime, minDistance, this);
} else {
// (Display a message/dialog) No Location Providers currently available.
}
}
/* Deactivates this provider.
* This method is automatically invoked by disabling my-location layer. */
@Override
public void deactivate() {
// Remove location updates from Location Manager
locationManager.removeUpdates(this);
mListener = null;
}
@Override
public void onLocationChanged(Location location) {
/* Push location updates to the registered listener..
* (this ensures that my-location layer will set the blue dot at the new/received location) */
if (mListener != null) {
mListener.onLocationChanged(location);
}
/* ..and Animate camera to center on that location !
* (the reason for we created this custom Location Source !) */
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
现在,在您的onMapReady(GoogleMap)
回调中,请将其放入:
public void onMapReady(GoogleMap map){
mMap = map;
FollowMeLocationSource locationSource = new FollowMeLocationSource();
locationSource.getBestAvailableProvider();
mMap.setLocationSource(locationSource);
mMap.setMyLocationEnabled(true);
}
根据需要修改此示例,但这应该可以为您提供所需的功能。