简体   繁体   English

Android-Google Maps-相机更新每个位置更改

[英]Android - Google Maps - Camera Update every location change

I'm building an app, which has to keep the user in the center of the map all the time. 我正在构建一个应用程序,该应用程序必须始终将用户保持在地图中心。

I have set up the following: 我设置了以下内容:

locationManager = (LocationManager) saveContext
        .getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MapLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,
        10, locationListener);
if (myLocation != null) {
    setLocation(myLocation);
}

private void setLocation(Location location) {
    workoutMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            location.getLatitude(), location.getLongitude()), 18.0f));
}

This works, as long I have a lastKnownLocation in my phone's history. 只要我的手机历史记录中有一个lastKnownLocation,就可以使用此功能。 The map will center correctly. 地图将正确居中。

I wish that every time I'm getting a new location update, setLocation() will be executed again, with the new location info. 我希望每次获取新的位置更新时,都会使用新的位置信息再次执行setLocation()。

I've tried creating a thread which just does get the lastKnownLocation and then setLocation(); 我试过创建一个线程,该线程确实会获取lastKnownLocation然后再setLocation();。 But this isn't working out. 但这还没有解决。

I suggest you take a look at the excellent and complete sample at Googles Receiving Location Updates guide. 建议您查看Google的“ 接收位置更新”指南中的完整示例。

It includes sample code and an Android project you can import in ADT straight away. 它包括示例代码和一个Android项目,您可以立即将其导入ADT。

The callback method that Location Services invokes to send a location update to your app is specified in the LocationListener interface, in the method onLocationChanged(). 在LocationListener界面的onLocationChanged()方法中指定了Location Services调用以向您的应用发送位置更新的回调方法。

There you can put your camera update. 您可以在此处放置相机更新。

Just in case anyone else stumbles on this, it can be done and the answer, given by user Rob, is in another StackOverflow post here . 万一其他人迷失了方向,可以做到这一点,而用户Rob给出的答案在此处的另一个StackOverflow帖子中。

The basic idea is to create a CameraPosition object, pass this to the CameraUpdateFactory and then have the map animate the CameraUpdate object. 基本思想是创建一个CameraPosition对象,将此对象传递给CameraUpdateFactory,然后使该地图动画化CameraUpdate对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM