1

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

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

     } 
    } 

回答

3

您可以在偵聽器中添加一個本地變量,並使用它來僅縮放第一個位置。該代碼將如下所示:

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

    } 
} 
+0

謝謝。有效。 – yams 2013-03-21 17:22:50

相關問題