2015-04-12 83 views
2

在谷歌地圖API V2在android應用程序中工作時,我想有兩個標記一個用於源和一個目標。我將全局引用設置爲兩個標記,當我移動到某個其他位置並使用destinationMarker.remove()刪除目標標記時,它不適用於我。它不會刪除上一個標記。.remove()不工作在Android谷歌地圖api v2

代碼:

//Global references 
    Marker sourceMarker; 
    Marker destinationMarker; 
    ... 
    ... 
    public void geoLocate(View v) throws IOException{ 
     hideSoftKeyBoard(v); 
     EditText et = (EditText)findViewById(R.id.editText1); 
     String Location = et.getText().toString(); 
     if(Location.length() == 0){ 
      Toast.makeText(this, "Please specify a Location", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     Geocoder gc = new Geocoder(this); 
     List<Address> list = gc.getFromLocationName(Location, 1); 
     Address add = list.get(0); 
     String locality = add.getLocality(); 
     Toast.makeText(this, locality, Toast.LENGTH_LONG).show(); 
     double lat = add.getLatitude(); 
     double lng = add.getLongitude(); 
     gotoLocation(lat, lng, DEFAULT_ZOOM); 
     if(destinationMarker != null){ 
     //removing marker if exists 
      destinationMarker.remove(); 
     } 
     MarkerOptions options = new MarkerOptions().title(locality).position(new LatLng(lat,lng)); 
     destinationMarker = mMap.addMarker(options); 

    } 
+0

試着用'mMap.clear();' – Rami

+1

我不想清除我的其他標誌物即sourceMarker –

回答

1

也許這是行不通的,因爲你將它添加和使用addMarker(..)方法給它一個引用之前移除標記。一旦使用addmarker(..)方法引用製造商,請刪除製造商。

MarkerOptions options = new MarkerOptions().title(locality).position(new LatLng(lat,lng)); 
     destinationMarker = mMap.addMarker(options); 
if(destinationMarker != null){ 
     //removing marker if exists 
      destinationMarker.remove(); 
     } 

    }