2016-04-10 16 views
0

しばらくマーカーの問題をソートしようとしていました。マップが更新されるたびに新しいマーカーが追加されます。同じ場所に複数のマーカーが残されます。Googleマップで更新ごとに新しいマーカーを追加する

私は、このような方向性のような他のコンテンツを追加する前に並べ替えたいです。

public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     return;// 
    } 
    mMap.setMyLocationEnabled(true); // shows location on map 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    listener = new LocationUpdateListener(); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
} 
private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 

    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 

    LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

    MarkerOptions options = new MarkerOptions() // This adds in a marker 
      .position(latLng) 
      .title("Reverse Geo Toast here ???"); // when marker clicked, it will display you are here 
    mMap.addMarker(options); 
    // mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    float zoomLevel = (float) 10; //This zooms into the marker 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel)); 
} 
+0

だからではなく、新しいものを追加することで、既存のマーカーの位置を更新したいですか? – antonio

答えて

1

あなたは(Reference)を使用して既存のマーカーの位置を更新することができます。

private Marker marker; 

// ... 

private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 

    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 

    LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

    if (marker == null) { 
     MarkerOptions options = new MarkerOptions() // This adds in a marker 
       .position(latLng) 
       .title("Reverse Geo Toast here ???"); // when marker clicked, it will display you are here 
     marker = mMap.addMarker(options); 
    } 
    else { 
     marker.setPosition(latLng); 
    } 
    // mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    float zoomLevel = (float) 10; //This zooms into the marker 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel)); 
} 
関連する問題