在我的Android应用程序中,我从JSON
收到纬度和经度,我已经使用标记在谷歌地图中指出了它,但我想在纬度和经度变化时移动标记,如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();
}
}
答案 0 :(得分: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);
}