我正在从共享首选项中获取每分钟经纬度,并在 map 上显示。
标记每分钟都在完美移动,但没有删除旧标记(它显示每分钟新标记。
- 但是关闭和打开 map fragment 它完美地显示了单个标记。
请帮我解决这个问题。我也试过 Marker.Remove
我在 OnLocation Changed Method 中调用了以下方法。
/* 在UI上显示位置的方法 */
private void displayLocation() { try { mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (mLastLocation != null) { double latitude = mLastLocation.getLatitude(); double longitude = mLastLocation.getLongitude(); // get user data from session HashMap<String, String> user = session.getGPSPING(); // UserLat String LatLongUser=""; LatLongUser = user.get(SessionManagerFor_Register.KEY_LATLONG); if(!LatLongUser.equals("")) { Log.i(" PING on MAP LatLong", LatLongUser); String[] LanlongArr = LatLongUser.split("//"); List<String> Lanlonglist1 = Arrays.asList(LanlongArr); int length = Lanlonglist1.size(); arraylist_DetailLineWalker = new ArrayList<String(length); for (int i = 0; i < length; i++) { arraylist_DetailLineWalker.add(Lanlonglist1.get(i)); } if(arraylist_DetailLineWalker!=null) { for (int i = 0; i < arraylist_DetailLineWalker.size(); i++) { try { String Val = arraylist_DetailLineWalker.get(i).toString(); //Log.i(" Validation Id",Val); VALUE_ARRAY_STRING = Val.toString().split("::"); LatLong_DataSaveTable = VALUE_ARRAY_STRING[0].toString(); System.out.println("checking STarted" + LatLong_DataSaveTable); String[] latlong = LatLong_DataSaveTable.split(","); double latitude1 = Double.parseDouble(latlong[0]); double longitude2 = Double.parseDouble(latlong[1]); //To hold location LatLng latLng1 = new LatLng(latitude1, longitude2); //To create marker in map MarkerOptions markerOptionsLineWalker = new MarkerOptions(); markerOptionsLineWalker.position(latLng1); //setting position markerOptionsLineWalker.draggable(true); //Making the marker draggable markerOptionsLineWalker.title("Walker Location"); markerOptionsLineWalker.icon(BitmapDescriptorFactory.fromResource(R.drawable.walker_outof_fence_icon_red)); Marker marker1 = googleMap.addMarker(markerOptionsLineWalker); if (marker1 != null) { marker1.remove(); } //adding marker to the map googleMap.addMarker(markerOptionsLineWalker); // marker1.setPosition(latLng1); Log.i(TAG, "Walker PING Added............................."); } catch (NumberFormatException e) { e.printStackTrace(); } } } else { Log.i("MAP NEwLatLong","TOTAL ARRY LIST NULLL"); } } else { Log.i("MAP NEwLatLong","Null Not LatLong"); } } else { Log.i("Location EXception","Couldn't get the location. Make sure location is enabled on the device"); // can't get location // GPS or Network is not enabled // Ask user to enable GPS/network in settings gps.showSettingsAlert(); } } catch (Exception e) { e.printStackTrace(); } }
最佳答案
试试这个....
Marker now;
@Override
public void onLocationChanged(Location location) {
if(now != null){
now.remove(); //if the marker is already added then remove it
}
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
now = googleMap.addMarker(new MarkerOptions().position(latLng)));
}
要引用,请访问此...
关于android - 每个分钟标记都平滑移动,但不会删除 android map 上的旧标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196013/