android - 如何在不更改用户设置的缩放比例的情况下更新 map 标记?

标签 android google-maps-api-3 google-maps-markers currentlocation

我有一个正在构建的 Android 应用程序,它使用带有位置监听器的 Google map 。本地图第一次出现时,我在位置监听器中将缩放设置为 12,我是 Google map 开发的新手,所以我想知道一旦用户捏住以更改缩放,我如何在不影响缩放的情况下更新位置?下面是我的位置监听器。

/**
 *Mylocationlistener class will give the current GPS location 
 *with the help of Location Listener interface 
 */
private class Mylocationlistener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
            // ---Get current location latitude, longitude---

            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
            currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
            Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
            if (!firstPass){
                currentLocationMarker.remove();
            }
            firstPass = false;
            Toast.makeText(MapViewActivity.this,"Latitude = "+
                    location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
                    Toast.LENGTH_LONG).show();

        }
    }

最佳答案

您可以在监听器中添加一个局部变量,并使用它仅缩放第一个位置。代码如下所示:

private class Mylocationlistener implements LocationListener {

   private boolean zoomed = false;

   @Override
   public void onLocationChanged(Location location) {

    if (location != null) {
        // ---Get current location latitude, longitude---

        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
        currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
        Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
        // Zoom in, animating the camera.
        if (!zoomed) {
            map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
            zoomed = true;
        }                                       
        if (!firstPass){
            currentLocationMarker.remove();
        }
        firstPass = false;
        Toast.makeText(MapViewActivity.this,"Latitude = "+
                location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
                Toast.LENGTH_LONG).show();

    }
}

关于android - 如何在不更改用户设置的缩放比例的情况下更新 map 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15550998/

相关文章:

android - 使用 CursorLoader 在 ListFragment 中查询 SQLite 数据库的最佳实践?

ios - Ionic 5 Google Map Marker 在 IOS 上打破动画

ios - 沿用户面向的方向旋转 GMSMarker

javascript - 谷歌地图/JS : How with a list of street address can we populate a Google Map?

java - Android:如何避免点击通知调用 onCreate()

android - 如何在 Android 的 ImageView 上画线?

android studio 不下载源代码和 javadoc

android - 地方 API 不工作。错误 : You must enable Billing on the Google Cloud Project

javascript - Google map 自动填充中的城市不是英文的

Android OnInfoWindowClickListener() 从未调用过