簡體   English   中英

如何在不更改用戶設置的縮放比例的情況下更新地圖標記?

[英]How do I update the map marker without changing the zoom that the user has set?

我有一個正在構建的 Android 應用程序,它使用帶有位置偵聽器的 Google 地圖。 當地圖第一次出現時,我在位置偵聽器中將縮放設置為 12,我在 Google Maps 開發中相當新,所以我想知道如何在用戶捏住更改縮放后更新位置而不影響縮放? 下面是我的位置監聽器。

/**
 *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();

    }
}

嘗試使用這個:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocationMarker.getPosition(), map.getCameraPosition().zoom));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM