Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌地图android:多个标记的实时更新位置_Android_Json_Google Maps_Google Maps Markers_Google Maps Android Api 2 - Fatal编程技术网

谷歌地图android:多个标记的实时更新位置

谷歌地图android:多个标记的实时更新位置,android,json,google-maps,google-maps-markers,google-maps-android-api-2,Android,Json,Google Maps,Google Maps Markers,Google Maps Android Api 2,我正在尝试创建一张地图,显示谷歌地图上指定人员的位置。我已经用MySQL数据库建立了一个web服务器。无论何时android应用程序运行,它都会在数据库中存储用户的当前位置。之后,每当调用onLocationChanged()时,应用程序都会获取距离用户位置(比如50公里)附近范围内(比如10人)的位置数据。使用这些位置坐标,我在地图上添加了多个标记。现在我面临以下问题: 当10个人的位置数据更改时,将创建新标记,而不是更改其位置的原始标记 当从数据库中删除10人中任何一人的位置数据时,相应的标

我正在尝试创建一张地图,显示
谷歌地图
上指定人员的位置。我已经用MySQL数据库建立了一个web服务器。无论何时android应用程序运行,它都会在数据库中存储用户的当前位置。之后,每当调用
onLocationChanged()
时,应用程序都会获取距离用户位置(比如50公里)附近范围内(比如10人)的位置数据。使用这些位置坐标,我在地图上添加了多个标记。现在我面临以下问题:

  • 当10个人的位置数据更改时,将创建新标记,而不是更改其位置的原始标记

  • 当从数据库中删除10人中任何一人的位置数据时,相应的标记仍保留在地图上

  • 注意:我使用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();
                }
    
            }
        } 
    
    @覆盖
    受保护的手部内容无效(意图){
    if(intent!=null){
    字符串json_result=intent.getExtras().getString(“json数据”);
    试一试{
    JSONObject JSONObject=新的JSONObject(json_结果);
    JSONArray JSONArray=jsonObject.getJSONArray(“位置数据”);
    int计数器=0;
    字符串名称,lat,lng;
    双区;
    内部排列长度;
    while(计数器
    下面是我如何使用解析的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);
    
                }
    
            }
        }
    
    私有类ParseJSONDataReceiver扩展了BroadcastReceiver{
    标记用户标记器;
    双lat、lng、dist;
    int numberOfConnections,id;
    HashMap HashMap=新的HashMap();
    @凌驾
    公共void onReceive(上下文、意图){
    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\u dist);
    numberOfConnections=intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_LENGTH);
    id=intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_id)+1;
    usersMarker=mMap.addMarker(新的MarkerOptions()
    .位置(新LatLng(lat,lng))
    .标题(“名称:”+名称)
    .代码段(id+“:距离:“+dist+”公里”)
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
    put(usersMarker,id);
    }
    }
    }
    

    有人能帮我解决上述问题吗???

    要更新现有标记的位置,您需要更改代码,然后使用
    HashMap
    而不是
    HashMap
    ,以便您可以通过用户id查询地图:

    HashMap<Integer, Marker> hashMap = new HashMap<>();
    
    如果要删除用户,还需要从映射和
    HashMap
    中删除其标记:

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

    你好,安东尼奥。非常感谢您的回复。我已经能够使用您建议的代码成功地更新标记的位置。但是,我不清楚如何使用“删除标记”代码。我的意思是hashmap如何知道某个特定条目是否已从MySQL数据库中删除??我尝试使用
    if(marker.getposition==null)
    但它不起作用@安东尼奥
    Marker marker = hashMap.get(id);
    if (marker!= null) {
        marker.remove();
        hashMap.remove(id);
    }