使相机随着位置的变化而移动(Google Maps Api)
Posted
技术标签:
【中文标题】使相机随着位置的变化而移动(Google Maps Api)【英文标题】:Make Camera move as Location changes (Google Maps Api) 【发布时间】:2016-04-26 20:09:23 【问题描述】:我对谷歌地图 api 有疑问。我想要做的是将相机与 Location 一起移动,以便蓝色指示器始终位于中心。
认为放置代码的好地方是在 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));
【问题讨论】:
这是另一个线程的解决方案:***.com/questions/13742551/… 为什么我不能将以下内容添加到 OnLocationChanged 事件中。:: @Override public void onLocationChanged(Location location) mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(10 * 1000) .setFastestInterval(1 * 1000); mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()))); 蓝色指示器位置请求是由地图实现的,与您的位置请求相比,它可以在不同的时间间隔给出不同的点。 AFAIK,无法收听地图使用的默认位置提供程序,您需要顺利更新地图摄像头位置。 好的,所以我不能按照我的更新@PabloBaxter 下的代码中显示的那样做 【参考方案1】:该代码可用于更新您的相机位置,但无法将 Google 地图蓝色指示器精确居中。这是因为蓝点正在侦听的 locationRequest 与您在代码 sn-p 中创建的 locationRequest 不同(这似乎没有做任何事情)。以下代码sn-p来自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);
根据需要修改此示例,但这应该会为您提供所需的功能。
【讨论】:
我会试试这个。因为就目前而言,我的代码并没有像你说的那样做任何事情。相机根本不动。 我已经测试了代码,我收到错误“java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String) ' 在“locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);”这一行的空对象引用上@PabloBaxter 您是否将 mContext 分配给包含您的地图的任何上下文? mContext 在那里。但是现在当我测试是否调用了 OnLocationChanged 时,它永远不会被调用。 放一个日志语句,并检查“activate()”是否被调用。另外,请检查您是否也在手机上启用了位置信息。以上是关于使相机随着位置的变化而移动(Google Maps Api)的主要内容,如果未能解决你的问题,请参考以下文章
将相机重定向到当前位置 Google Maps API v2
Flutter 中的 Google Maps 相机位置更新问题