我从共享首选项中获取每分钟LatLong ,在地图上显示。
标记在每分钟完美移动,但不会移除旧标记(显示每分钟新标记。
请帮我解决这个问题。另外我试过 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();
}
}
}
答案 0 :(得分:3)
试试这个......
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)));
}
参考,请访问此...