我想删除一个标记,它会保持相乘,因为位置已更改我尝试过map.clear()但它也会删除我绘制的折线和marker.remove但它停止显示,我也想要实施另一个标记,显示我的GPS位置,并获得这些标记之间的距离,有谁知道我怎么能这样做?谢谢!
这是我的代码
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 :(得分:2)
为了防止当前位置标记被复制,只要在每次新位置进入时它都不为空,只需在标记引用上调用remove()
:
@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"));
}
请注意使用GoogleMap位置监听器is deprecated,因此您应切换到使用FusedLocationProviderAPI see here for a complete example。
至于获取两个标记之间的距离,您可以使用Location.distanceBetween()
方法。
如果您有两个标记:
Marker marker1;
Marker marker2;
以下是如何获得它们之间的距离:
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
}