2016-03-28 10 views
0

Ich möchte eine Markierung löschen, die es multipliziert wegen der Position ändert Ich habe versucht map.clear(), aber es löscht auch die Polylinie, die ich zeichnete und marker.remove, aber es zeigt nicht mehr Ich möchte auch einen anderen Marker implementieren, der mir einen GPS-Standort zeigt und den Abstand zwischen diesen Markern ermittelt. Weiß jemand, wie ich das machen kann? Vielen Dank!mehrere Marker auf Benutzerstandort Android

Dies ist mein Code

package com.example.alejandro.integradora2; 

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.graphics.Color; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.maps.model.Polyline; 
import com.google.android.gms.maps.model.PolylineOptions; 

public class Route extends AppCompatActivity implements OnMapReadyCallback { 

    public GoogleMap mMap; 
    Marker marker; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     setUpMapIfNeeded(); 

    } 
//here is the marker that keeps multiplying 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      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 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
      mMap.setMyLocationEnabled(true); 

      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 


       mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

        @Override 
        public void onMyLocationChange(Location arg0) { 
         // TODO Auto-generated method stub 

        marker= mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Tu Ubicacion Actual")); 

        } 
       }); 

      } 
     } 
    } 

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

     // Add a marker in Sydney and move the camera 
     LatLng inicio = new LatLng(23.979804, -104.641936); 
     LatLng fin = new LatLng(24.024227, -104.664647); 
     mMap.addMarker(new MarkerOptions().position(inicio).title("Inicio Ruta").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); 
     mMap.addMarker(new MarkerOptions().position(fin).title("Fin Ruta")); 
     //mMap.animateCamera(CameraUpdateFactory.zoomTo(10)); 

     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.979804, -104.641936), 12)); 

     PolylineOptions RutaIda = new PolylineOptions() 
       .add(new LatLng(23.979804, -104.641936),new LatLng(23.986028, -104.663826),new LatLng(23.986337, -104.664357),new LatLng(23.996928, -104.662372),new LatLng(23.997051, -104.662469), 
         new LatLng(23.998414, -104.662179),new LatLng(24.007054, -104.662335),new LatLng(24.008568, -104.662292),new LatLng(24.015409, -104.663338),new LatLng(24.017710, -104.663729), 
         new LatLng(24.019288, -104.664051),new LatLng(24.023110, -104.663981),new LatLng(24.024296, -104.664153),new LatLng(24.024227, -104.664647) 
         ) 
       .width(5) 
       .color(Color.RED); 
     Polyline polyline = mMap.addPolyline(RutaIda); 

     PolylineOptions RutaVuelta = new PolylineOptions() 
       .add(new LatLng(24.024227, -104.664647),new LatLng(24.023497, -104.669067),new LatLng(24.021361, -104.668826),new LatLng(24.021430, -104.668161),new LatLng(24.019950, -104.667893), 
         new LatLng(24.019666, -104.665200),new LatLng(24.019490, -104.664937),new LatLng(24.019431, -104.664524),new LatLng(24.018319, -104.664030),new LatLng(24.016864, -104.664062), 
         new LatLng(24.015170, -104.663458),new LatLng(24.008048, -104.662445),new LatLng(23.998369, -104.662447),new LatLng(23.993684, -104.663177),new LatLng(23.986396, -104.664572), 
         new LatLng(23.986318, -104.664277),new LatLng(23.986053, -104.663848),new LatLng(23.979804, -104.641936)) 
       .width(8) 
       .color(Color.parseColor("#802E2EFE")); 
     //.color(Color.BLUE); 





     Polyline polylinevenida = mMap.addPolyline(RutaVuelta); 

    } 





} 
+0

Es sieht nicht wie dieser Code ruft marker.remove() – nasch

+0

Ich löschte es, weil es die Markierung von –

Antwort

2

Denn von den aktuellen Standort Marker verhindert dupliziert, rufen Sie einfach remove() auf der Marker Referenz, wenn es einen neuen Standort in nicht null jeder Zeit kommt:

@Override 
public void onMyLocationChange(Location arg0) { 
     //Add this: 
     if (marker != null) { 
      marker.remove(); 
     } 

     marker= mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Tu Ubicacion Actual")); 

} 

Bitte beachten Sie jedoch, dass Sie mit dem GoogleMap-Standort-Listener is deprecated die Option FusedLocationProviderAPI, see here for a complete example, verwenden sollten.

Um den Abstand zwischen zwei Markern zu erhalten, können Sie die Methode Location.distanceBetween() verwenden.

Wenn Sie zwei Markierungen haben:

Marker marker1; 
Marker marker2; 

Hier ist, wie man den Abstand zwischen ihnen zu bekommen:

LatLng latLng1 = marker1.getPosition(); 
LatLng latLng2 = marker2.getPosition(); 

float[] distance = new float[2]; 

Location.distanceBetween(latLng1.latitude, latLng1.longitude, 
     latLng2.latitude, latLng2.longitude, distance); 

if(distance[0] < 100){ 
    // do something if distance between 
    // is less than 100 meters 
} 
+0

gestoppt hat Danke Daniel Ich habe gesehen, dass ich FusedLocationProviderAPI verwenden sollte, aber ich bin neu zu Android und ich weiß nicht, wie man es benutzt, ich werde diese Verbindungen überprüfen, noch eine Frage..mit FusedLocationProviderAPI kann ich auch bekommen die Entfernung zwischen 2 Standorten? Einer ist ein GPS-Tracker? Nochmals vielen Dank –

+0

Die Entfernung zwischen zwei Punkten ist einfach, werden Sie den Abstand zwischen zwei Marker-Objekte vergleichen? –

+0

Ich werde versuchen, Ihnen zu erklären, was ich versuche zu tun: Ich mache eine App, die eine Polylinie zeigt und ich möchte Benutzer Standort und zeigen Sie den Standort eines GPS und wie das GPS kommt näher an die Benutzer Standort, zeigt es in der App an, wie weit es voneinander entfernt ist und dass es das GPS durch die Polylinie zeigt ... ist es zu kompliziert? –