android - 谷歌地图安卓: Live update position of multiple markers

标签 android json google-maps google-maps-markers google-maps-android-api-2

我正在尝试创建一个 map ,显示指定人员在 Google map 上的位置。我已经设置了一个带有 MySQL 数据库的 Web 服务器。每当 Android 应用程序运行时,它都会将用户的当前位置存储在数据库中。之后,每当调用onLocationChanged()时,应用程序都会获取用户附近范围(例如,50Km)内(例如,10 个人)的位置数据地点。使用这些位置坐标,我在 map 上添加了多个标记。现在我面临以下问题:

  1. 当 10 个人的位置数据发生变化时,将创建新的标记,而不是改变原始标记的位置。

  2. 当这 10 个人中任何一个的位置数据从数据库中删除时,相应的标记仍保留在 map 上。

注意:我使用 JSON 从服务器获取数据,并在 IntentService 中解析 JSON 数据以创建标记。

这是我解析 JSON 的方法:

@Override
    protected void onHandleIntent(Intent intent) {

        if (intent != null) {

            String json_result = intent.getExtras().getString("JSON Data");
            try {
                JSONObject jsonObject = new JSONObject(json_result);
                JSONArray jsonArray = jsonObject.getJSONArray("location data");

                int counter = 0;
                String name, lat, lng;
                double dist;
                int arrayLength;

                while (counter < jsonArray.length()) {

                    JSONObject object = jsonArray.getJSONObject(counter);
                    name = object.getString("name");
                    lat = object.getString("lat");
                    lng = object.getString("lng");
                    dist = object.getDouble("distance");
                    arrayLength = jsonArray.length();

                    Intent jsonDataIntent = new Intent();
                    jsonDataIntent.setAction(Constants.STRING_ACTION);
                    jsonDataIntent.putExtra(Constants.STRING_NAME, name);
                    jsonDataIntent.putExtra(Constants.STRING_LAT, lat);
                    jsonDataIntent.putExtra(Constants.STRING_LNG, lng);
                    jsonDataIntent.putExtra(Constants.STRING_DIST, dist);
                    jsonDataIntent.putExtra(Constants.STRING_LENGTH, arrayLength);
                    jsonDataIntent.putExtra(Constants.STRING_ID, counter);
                    LocalBroadcastManager.getInstance(this).sendBroadcast(jsonDataIntent);


                    counter++;
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    } 

以下是我如何使用解析的 JSON 数据来创建标记:

private class ParseJSONDataReceiver extends BroadcastReceiver {

        Marker usersMarker;
        double lat, lng, dist;
        int numberOfConnections, id;
        HashMap<Marker,Integer> hashMap = new HashMap<>();

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent != null) {

                String name = intent.getExtras().getString(ParseJSONDataService.Constants.STRING_NAME);
                lat = Double.parseDouble(intent.getExtras().getString(ParseJSONDataService.Constants.STRING_LAT));
                lng = Double.parseDouble(intent.getExtras().getString(ParseJSONDataService.Constants.STRING_LNG));
                dist = intent.getExtras().getDouble(ParseJSONDataService.Constants.STRING_DIST);
                numberOfConnections = intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_LENGTH);
                id = intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_ID) + 1;


                usersMarker = mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(lat, lng))
                        .title("Name: " + name)
                        .snippet(id + ": Distance: " + dist + " Km")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                hashMap.put(usersMarker,id);

            }

        }
    }

谁能帮我解决以上问题吗???

最佳答案

要更新现有标记的位置,您需要更改代码,使用 HashMap<Integer, Marker>而不是HashMap<Marker, Integer>这样您就可以通过用户 ID 查询您的 map :

HashMap<Integer, Marker> hashMap = new HashMap<>();

然后,您可以像这样添加我们的标记(更新现有标记):

if (hashMap.containsKey(id)) {
    Marker marker = hashMap.get(id);
    marker.setPosition(new LatLng(lat, lng)); // Update your marker
} else {
    usersMarker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lng))
            .title("Name: " + name)
            .snippet(id + ": Distance: " + dist + " Km")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

    hashMap.put(id, usersMarker);
}

如果您想删除用户,您还需要从 map 和 HashMap 中删除其标记。 :

Marker marker = hashMap.get(id);
if (marker!= null) {
    marker.remove();
    hashMap.remove(id);
}

关于android - 谷歌地图安卓: Live update position of multiple markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38583701/

相关文章:

javascript - 如何通过 ID 选择和操作谷歌地图(V3 JS API)标记?

javascript - 用 panby 偏移谷歌地图中心

android - 将 subview 添加到相对布局

javascript - 防止 jquery 和/或 x-editable 将字符串解析为对象

java - 如何使用 GSON 解析复杂的 JSON 字符串与 JSON 数组的混合?

c++ - QT 中的 json 解析器

android - 我如何使用 google places 在 android 中查找位置

java - Android:如何在 Crouton onDisplayed() 上使用回调

安卓蓝牙连接心率监测器

android - 针对多次访问尝试的 SQLCipher 安全性 (Android)