当前位置: 首页 > 知识库问答 >
问题:

当经纬度改变时,在Google地图中移动标记

唐向荣
2023-03-14

在我的应用程序中创建一个数组列表,其中包含JSON的纬度和经度,并使用数组列表在地图中添加标记,但当纬度长改变标记从一个位置移动到另一个位置时,它会创建两个标记而不是移动,如果我给map.clear(),整个标记会禁用并启用,但我想删除旧标记,如果不使用map.clear(),这是怎么可能的,请帮助

需要专用void setupmapifneed(视图膨胀视图){

        if (mMap == null) { 
            mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap(); 
            mMap.setMyLocationEnabled(true); 

            Location myLocation = mMap.getMyLocation(); 
            if (mMap != null) {  
                //mMap.clear();
                
                // setUpMap(); 
                mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

                    @Override 
                    public void onMyLocationChange(Location arg0) { 

                        // TODO Auto-generated method stub 
                        final LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
                     mMap.clear();
                        if (marker != null) {
                             marker.remove();
                         }
                        for (int i = 0; i < arraylist1.size(); i++) { 
                            final LatLng position = new LatLng(Double 
                                    .parseDouble(arraylist1.get(i).get("Latitude")), 
                                    Double.parseDouble(arraylist1.get(i).get( 
                                            "Longitude"))); 
                            String ime1 = arraylist1.get(i).get("IME");

                            final MarkerOptions options = new MarkerOptions() 
                            .position(position); 
                            //mMap.addMarker(options); 
                            //mMap.addMarker(options.icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));
                            
                              marker=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));

                            //options.title(ime1);
                            builder.include(position); 
                        
                        } 
                        LatLng latLng = new LatLng(arg0.getLatitude(), arg0 
                                .getLongitude()); 
                        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        mMap.setMyLocationEnabled(true); 

                        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
                        // mMap.setOnMapClickListener(null); 
                        mMap.setOnMarkerClickListener(null); 

                        mMap.animateCamera(CameraUpdateFactory.zoomTo(9)); 
                    } 
                }); 

            } 
        } 
    } 

    /*  protected void retrieveAndAddCities() throws IOException { 
        HttpURLConnection conn = null; 
        final StringBuilder json = new StringBuilder(); 
        try { 

            URL url = new URL(SERVICE_URL); 
            conn = (HttpURLConnection) url.openConnection(); 
            InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

            int read; 
            char[] buff = new char[1024]; 
            while ((read = in.read(buff)) != -1) { 
                json.append(buff, 0, read); 
            } 
        } catch (IOException e) { 
            Log.e(LOG_TAG, "Error connecting to service", e); 
            throw new IOException("Error connecting to service", e); 
        } finally { 
            if (conn != null) { 
                conn.disconnect(); 
            } 
        } 
        new DownloadJSON().execute(); 

    } */

    private class DownloadJSON extends AsyncTask<Void, Void, Void> { 
        String result=""; 
        @Override 
        protected void onPreExecute() { 
            super.onPreExecute(); 
        }

        @Override 
        protected Void doInBackground(Void... params) { 
            try { 
                arraylist1 = new ArrayList<HashMap<String, String>>(); 

                JSONParser jParser = new JSONParser(); 
                String result = ""; 

                json = jParser.getJSONFromUrl(SERVICE_URL); 

                try { 
                    //arraylist1.clear(); 

                    jsonarray = json.getJSONArray("SingleIMEs"); 
                    Log.d("Haaaaaaaaaaaa", "" + json); 

                    for (int i = 0; i < jsonarray.length(); i++) { 
                        Log.d("H11111111111111111111111111", 
                                "" + jsonarray.length()); 
                        map = new HashMap<String, String>(); 
                        json = jsonarray.getJSONObject(i); 

                        // pubname = json.getString("PubName"); 
                        latitude = json.getDouble("Latitude"); 
                        longitude = json.getDouble("Longitude"); 
                        ime = json.getString("IME");
                        //  map.put("PubName", json.getString("PubName")); 
                        //map.put("PubID", json.getString("PubID")); 
                        map.put("Latitude", json.getString("Latitude")); 
                        Log.e("CHECKLAT",""+json.getString("Latitude") );
                        map.put("Longitude", json.getString("Longitude")); 
                        Log.e("CHECKLONG",""+json.getString("Longitude") );

                        map.put("IME", json.getString("IME"));
                        arraylist1.add(map); 
                    } 

                } catch (JSONException e) { 
                    Log.e("log_tag", "Error parsing data " + e.toString()); 
                    result="Error"; 
                    e.printStackTrace(); 
                } 
            }catch(Exception e){ 
                result="Error"; 
            } 

            return null; 
        } 

        protected void onPostExecute(Void args) { 

            // mProgressDialog.dismiss(); 

        } 
    } 

    @Override 
    public void onResume() { 
        super.onResume(); 
        mMapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
        super.onPause(); 
        mMapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
        mMapView.onDestroy(); 
    super.onDestroy(); 
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

   /* Toast.makeText(getActivity(), "onLocationUpdated!!!", Toast.LENGTH_SHORT).show();
        Log.d("onLocationUpdated!!!","");
        new DownloadJSON().execute(); 
        setUpMapIfNeeded(rootView);*/

    }
    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {

        // TODO Auto-generated method stub

    } 
    

}

共有2个答案

叶阳
2023-03-14

创建标记时,需要保留所创建的所有标记,因为绘制后无法从地图中获取特定标记。然后,您所要做的就是从列表中找到标记并更新它。

我使用HashMap来存储标记,以便通过一些键快速引用它们

谭炎彬
2023-03-14

首先,您需要以某种方式识别您的标记,每个标记都必须有一个uniq id,假设它是一个String

然后你需要储存你的标记。

private HashMap<String, Marker> mMarkers = new HashMap<>();

然后,在使用新数据更新标记之前,您需要复制以前的标记集合,以找出新添加的标记和缺少的标记:

HashMap<String, Marker> oldMarkers = mMarkers;
mMarkers = new HashMap<>();

for (YourMarkerObject m : newMarkers) { // I assume you have collections of your objects here
  String id = m.getId(); // Get an id
  Marker marker = oldMarkers.get(id);
  if (marker == null) { // it means it's the new one
    // Create marker and attach it to the map on the right position
  } else { // it means we already have this marker attached, so just move it
    marker.setPosition(m.getPosition());
    oldMarkers.remove(id); // Remove this one from old markers collection
  }

  mMarkers.put(id, marker);
}

for (Marker oldMarker : oldMarkers.values()) {
  // Detach old markers
}
 类似资料:
  • 本文向大家介绍百度地图经纬度转换到腾讯地图/Google 对应的经纬度,包括了百度地图经纬度转换到腾讯地图/Google 对应的经纬度的使用技巧和注意事项,需要的朋友参考一下 实现目的:将百度地图经纬度 转换到 腾讯地图/Google 对应的经纬度. 方法1:使用代码进行转换 存在的问题:转换之后误差大,基本不可用 方法2: 该网站提供转换服务,坐标较为准确,可用,后台调用没有仔细研究 http:

  • 我正在用html/jsp页面主体中的Google地图做一个web动态项目。 我制作了一个函数,可以创建一个标记(lat、lng、map),并在标记的参数中使用一个特殊的image.png作为图标。 在我的地图中,我制作了两种不同的样式(地图的颜色…)“白天”和“夜晚”。 我想知道当用户点击夜间更改样式时,我如何更改标记的图标。事实上,标记的颜色不适合这种风格的地图... 我尝试用相同的名称初始化不

  • 问题内容: 尝试通过sql数据库中的经/纬度坐标间隔移动标记/地图。 在moveMarkerMap()中设置新的google.maps.LatLng(14,41)会移动它,并且returnData显示在alert()中,但与moveMarkerMap()一起使用时标记不会移动 从ajax返回的字符串格式正确;(9.624672,7.242244)如alert()所示,因此不确定为什么它不起作用。

  • 我正在使用下面的代码创建一个地图并为其附加一个标记。我还添加了一个标记监听器,在那里我需要在拖动后获得标记位置的经度和纬度。 它所做的是返回我的当前位置,而不是拖动后标记的位置。这部分有什么帮助吗?!

  • 这些是我找到的链接谷歌地图:移动标记和地图一起顺利与用户? 如何在Google Maps v2 android中平稳地移动当前位置标记