【问题标题】:Why google map v2 showing so many markers?为什么谷歌地图 v2 显示这么多标记?
【发布时间】:2015-04-19 22:29:13
【问题描述】:

您好,我有这个问题,谷歌地图 v2 显示了这么多标记,我只想要带有我确切位置的标记。我怎样才能删除它?我的猜测是它不能真正确定我的确切位置?这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    // show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        finish();
    }
    setContentView(R.layout.main);
    SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.googleMap);
    googleMap = supportMapFragment.getMap();
    googleMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(bestProvider);

    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);



}

@Override
public void onLocationChanged(Location location) {


    Button btnOurLocation = (Button) findViewById(R.id.latlongLocation);
    // TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    googleMap.addMarker(new MarkerOptions().position(latLng));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    // locationTv.setText("Latitude:" + latitude + ", Longitude:" +
    // longitude);

    try {
        geocoder = new Geocoder(StopItActivity.this, Locale.ENGLISH);
        addresses = geocoder.getFromLocation(latitude, longitude, 1);
        StringBuilder str = new StringBuilder();

        // Address returnAddress = addresses.get(0);

        address = addresses.get(0).getAddressLine(0);
        // String localityString = returnAddress.getLocality();
        // String city = returnAddress.getCountryName();
        // String region_code = returnAddress.getCountryCode();
        // String zipcode = returnAddress.getPostalCode();

        // str.append(address + "");
        // str.append(localityString + "");
        // str.append(city + "" + region_code + "");
        // str.append(zipcode + "");

    } catch (IOException e) {
        // TODO Auto-generated catch block

        Log.e("tag", e.getMessage());
    }

    btnOurLocation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            Toast.makeText(getApplicationContext(), address,
                    Toast.LENGTH_SHORT).show();

        }

    });

}

这是输出

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    我认为您应该在添加新标记之前删除旧标记。在 OnLocationChanged(Location location) 回调中添加标记之前添加以下代码行。

     googleMap.clear()
    

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 2014-11-08
      • 2016-08-31
      • 2015-03-14
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多