【问题标题】:How to move marker when lat long changes in Google map android?当谷歌地图android中的经纬度变化时如何移动标记?
【发布时间】:2015-10-11 19:59:47
【问题描述】:

在我的 android 应用程序中,我收到来自 JSON 的纬度和经度,我已经使用标记在 Google 地图中指出了它,但是我想在纬度和经度发生变化时移动标记,例如 GPS 跟踪?请帮帮我,这是我的源代码,............

 @Override
    protected Boolean doInBackground(String... urls) {
        try {


            List<Marker> markers = new ArrayList<Marker>();

            // Date a = new Date();
            // a.setTime(System.currentTimeMillis()-(60*60*1000));
            // Log.e("onehourback",""+a);*/
            // ------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("SingleIMEs");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    // Log.e("object",""+object);
                    /*
                     * try { array.add(jarray.getJSONObject(i));
                     * 
                     * Log.e("array",""+array); } catch (JSONException e) {
                     * e.printStackTrace(); }
                     */
                    latvalue = object.getString("Latitude");
                    longvalue = object.getString("Longitude");
                    latt = Double.parseDouble(latvalue);
                    lng = Double.parseDouble(longvalue);
                    Log.e("lat", "" + latt);
                    Log.e("lon", "" + lng);

                    Marker marker1 = mMap.addMarker(new MarkerOptions().position(new LatLng(latt, lng))); 
                    markers.add(marker1);

                    Log.e("asdf",";lkj"+markers);

                    /*
                     * for(int j=0; j < 1;j++) {
                     * 
                     * 
                     * 
                     * 
                     * }
                     */
                }
            }
            // }
            return true;

            // ------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        }

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

        return false;
    }

    protected void onPostExecute(Boolean result) {
        // dialog.cancel();
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        /*
         * adapter.notifyDataSetChanged(); if(result == false)
         * Toast.makeText(getActivity().getApplicationContext(),
         * "Unable to fetch data from server", Toast.LENGTH_LONG).show();
         */
    }

}

private void setUpMapIfNeeded(View inflatedView) {
    mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap();
    mMap.addMarker(new MarkerOptions().position(new LatLng(latt, lng))
            .title("WePOP"));
    mMap.setMyLocationEnabled(true);

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

            @Override
            public void onMyLocationChange(Location arg0) {
                // TODO Auto-generated method stub
                LatLng latLng = new LatLng(arg0.getLatitude(), arg0
                        .getLongitude());
                mMap.clear();
                mMap.addMarker(new MarkerOptions()
                .position(
                        new LatLng(arg0.getLatitude(), arg0
                                .getLongitude())).title("WePOP"));

                // mMap.addMarker(new
                // MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(
                // new LatLng(Double.parseDouble(datas.get("Lat")),
                // Double.parseDouble(datas.get("Long")))));
                //mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                //mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

            }
        });

    }
}

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

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

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

}

【问题讨论】:

  • 删除旧的标记并添加新的标记
  • @MD: 请给我一个示例代码

标签: android google-maps


【解决方案1】:

从您当前的代码中,您只是将一个标记覆盖在另一个标记之上。为了避免这种情况,只需清除旧标记并绘制一个新标记,

                public void onMyLocationChange(Location arg0) { 
                    // TODO Auto-generated method stub 
                    LatLng latLng = new LatLng(arg0.getLatitude(), arg0.getLongitude()); 
                    // clear the marker here
                    mMap.clear();
                    mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("WePOP")); 

                    //   mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position( new LatLng(Double.parseDouble(datas.get("Lat")), Double.parseDouble(datas.get("Long"))))); 
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 


                } 

更新:

为了跟踪多个标记,最好的方法是创建一个标记数组并在位置更改后加载它们。

   // first create a maker array
   List<Marker> markers = new ArrayList<Marker>();

   // inside doInBackground() add the markers into the list from JSON
   Marker marker1 = mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng))); 
   markers.add(marker);

现在您可以保留 JSON 中的标记并添加新的位置标记

  public void onMyLocationChange(Location arg0) { 
    // clear the marker here
    mMap.clear();
    LatLng latLng = new LatLng(arg0.getLatitude(), arg0.getLongitude()); 
    // check if marker1 is set or available in markers array and load the old marker first
    ............
    // now load the newest marker from location or save them into the markers array for further use
    Marker marker2 = mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("WePOP")); 
    markers.add(marker2);

   } 

【讨论】:

  • 如果我清除标记,标记仅指向当前位置,但在我的应用程序中,我还想显示 JSON 中的当前位置和纬度经度,所以如果我清除标记,它不会显示纬度和经度。
  • 是的,实际上它是一个跟踪应用程序显示,当屏幕打开时,它希望使用标记从 JSON 中指向当前位置和纬度和经度,如果纬度和经度发生变化,标记应该移动
  • Prokash Sarkar:我尝试通过为一组标记创建数组列表并按照您上面给出的那样绘制标记,但是应用程序崩溃了,请您帮帮我
猜你喜欢
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
相关资源
最近更新 更多