如何在Google map android中长时间更改标记时移动标记? [英] How to move marker when lat long changes in Google map android?

查看:87
本文介绍了如何在Google map android中长时间更改标记时移动标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用程序中,我从 JSON 接收到经纬度,并且我使用标记将它指向Google地图,但是我想在纬度和经度变化如 GPS 跟踪??请帮助我这里是我的源代码.........

in my android application I am receiving the latitude and longitude from JSON and I have pointed it in Google map using marker but I want to move the marker when the latitude and longitude changes like GPS tracking?? Please help me here is my source code,.........

 @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();
}

}

}

推荐答案

从您当前的代码中,您只需重写另一个标记。为避免这种情况,只需清除旧标记并绘制一个新标记,

From your current code you are simply overriding a marker on top of another. To avoid this simply clear the old marker and draw a new one,

                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)); 


                } 

更新:

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

For keeping track of multiple markers the best way would be creating a marker array and load them after the location change.

   // 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的标记并添加新的位置标记

Now you can keep the markers from JSON and add the new location marker

  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);

   } 

这篇关于如何在Google map android中长时间更改标记时移动标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆