我的应用中有一个活动,其中包含Map
,当用户从其设备中打开此活动时,会添加一个标记,显示其当前位置,然后是内部Service
类更新实时位置的代码就是。
这是我的代码:
mDatabase.child(rID).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Map<String, String> newAcceptedUser = (Map<String, String>) dataSnapshot.getValue();
pAccepted = newAcceptedUser.get("pName");
uidPAccepted = newAcceptedUser.get("pUrl");
String cLatS = newAcceptedUser.get("currentLat").trim();
currentLtAU = Double.parseDouble(cLatS);
String cLngS = newAcceptedUser.get("currentLng").trim();
currentLnAU = Double.parseDouble(cLngS);
Toast.makeText(getBaseContext(), pAccepted.trim() + " joined", Toast.LENGTH_LONG).show();
pMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(currentLtAU, currentLnAU)).title(pAccepted.trim()));
venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title("Play at " + venue.trim()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
markersList.add(pMarker);
markersList.add(venueMarker);
////get the latLngbuilder from the marker list
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
//Bounds padding here
int padding = 100;
//Create bounds here
LatLngBounds bounds = builder.build();
//Create camera with bounds
final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
startService(new Intent(getBaseContext(), ALocationService.class));
//Check map is loaded
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
mMap.animateCamera(cu);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.setMaxZoomPreference(19.0f);
}
});
} else {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Some error occurred. Please retry!", Snackbar.LENGTH_SHORT);
snackbar.show();
}
}
...
...
});
此处ALocationService.class
:
public static class ALocationService extends Service {
...
@Override
public void onCreate() {
super.onCreate();
locationRequest = LocationRequest.create() //standard GMS LocationRequest
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setNumUpdates(1000)
.setInterval(1000);
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
subscription = locationProvider.getUpdatedLocation(locationRequest)
.subscribe(new Action1<Location>() {
@Override
public void call(Location location) {
currentLtAU = location.getLatitude();
currentLnAU = location.getLongitude();
if(pMarker!=null)
{
pMarker.remove();
}
pMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(currentLtAU, currentLnAU)).title(pAccepted.trim()));
markersList.add(pMarker);
}
...
}
此处onBackPressed()
:
@Override
public void onBackPressed() {
new AlertDialog.Builder(AcceptedRequest.this)
.setMessage("Are you sure you want to quit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), pAccepted.trim() + " left", Toast.LENGTH_LONG).show();
markersList.remove(pMarker);
markersList.remove(venueMarker);
pMarker.remove();
venueMarker.remove();
stopService(new Intent(getBaseContext(), ALocationService.class));
subscription.unsubscribe();
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
}
以下是数据结构的样子:
-app
-child
-rID
-uniqueId
-key: value
-key: value
-key: value
-uniqueId
-key: value
-key: value
-key: value
-uniqueId
-key: value
-key: value
-key: value
问题是当不同的用户从他们的设备打开此活动时,会发生一些奇怪的事情。有时候,venueMarker
会被重命名为pMarker
,或者对于一个用户,2-3个标记会立即被添加,或者用户无法看到更新地图上其他用户的实时位置以及何时按下后退按钮,此Toast.makeText(getBaseContext(), pAccepted.trim() + " left", Toast.LENGTH_LONG).show();
显示其他用户的名称,而不是已离开的用户。
这里非常复杂。 如果你可以建议我一些简单的方法,或者通过改进我的代码并让我知道这些问题,它真的很有帮助!