如何在用户打开活动时在地图上添加标记,并在用户移动时更新每个标记的实时位置?

2024-04-21

我的应用程序中有一个活动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();
                }
            }
            ...
            ...
        });

Here's 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);
        }
        ...
    }

Here's 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();显示其他用户的姓名,而不是已离开的用户的姓名。

这里一切都非常复杂。如果您可以建议我一些简单的方法来做到这一点,或者改进我的代码并让我知道问题!


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在用户打开活动时在地图上添加标记,并在用户移动时更新每个标记的实时位置? 的相关文章

随机推荐