【问题标题】:multiple markers on user location Android用户位置Android上的多个标记
【发布时间】:2016-03-29 13:03:16
【问题描述】:

我想删除一个由于位置而不断增加的标记 我试过 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);

    }





}

【问题讨论】:

  • 这段代码看起来不像是在调用 marker.remove()
  • 我删除了它,因为它阻止了标记的显示

标签: android google-maps android-studio


【解决方案1】:

为了防止当前位置标记被复制,只需在标记引用上调用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
}

【讨论】:

  • 谢谢 Daniel 我已经看到我应该使用 FusedLocationProviderAPI 但我是 Android 新手,我不知道如何使用它我要查看这些链接,还有一个问题..with FusedLocationProviderAPI 我还可以获得两个位置之间的距离吗?一个是 GPS 追踪器?再次感谢您
  • 获取两点之间的距离很容易,您会比较两个 Marker 对象之间的距离吗?
  • 我将尝试向您解释我正在尝试做什么:我正在做一个显示折线的应用程序,我想获取用户位置并显示 GPS 的位置,并且作为GPS 离用户位置更近了,它在应用程序中指示它与另一个位置的距离,并显示 GPS 穿过折线……是不是太复杂了?
  • @AlejandroGurrola 我刚刚更新了答案。另外,您应该看看我的另一个答案,听起来它可以帮助您进一步实施:stackoverflow.com/questions/30431230/…
  • 非常感谢!!我会看看它再次谢谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2016-11-29
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多