这是我解析json数组的代码。我在日志中获得了正确的值,但是解析后的值标记未显示在地图上。请检查我的代码并告诉我哪里出错了。?
这是我的代码,
public class NearActivity extends Activity implements LocationListener {
GoogleMap gMap;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_near);
gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
new HttpGetTask().execute();
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
location.getLatitude(), location.getLongitude()), 14));
gMap.addMarker(new MarkerOptions()
.position(
new LatLng(location.getLatitude(), location
.getLongitude())).title("I'm here"));
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.near, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private class HttpGetTask extends AsyncTask<Void, Void, String> {
Double lat = locationManager.getLastKnownLocation(provider)
.getLatitude();
Double lng = locationManager.getLastKnownLocation(provider)
.getLongitude();
String URL = "http://latlong.in/api/stores/36/around_me.json?app_id=test&lat="
+ lat + "&lng=" + lng;
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected String doInBackground(Void... params) {
HttpGet request = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
JSONArray json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
Log.v("Response", result);
JSONObject e = json.getJSONObject(i);
String point = e.getString("point");
Log.v("POINT", point);
String[] point2 = point.split(",");
double lat1 = Double.parseDouble(point2[0]);
double lng1 = Double.parseDouble(point2[1]);
Log.v("LLDN", "" + lat1 + "&" + lng1);
gMap.addMarker(new MarkerOptions().title(
e.getString("name")).position(
new LatLng(lat1, lng1)));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != mClient)
mClient.close();
}
}
这是我的json回复
[
{
point: "77.606706,12.967060",
distance: "1.41",
id: 5686,
ph: "1234",
name: "zxcvb",
LS: " asdfrewrtet"
},
{
point: "77.606706,12.967060",
distance: "1.41",
id: 5686,
ph: "1234",
name: "zxcvb",
LS: " asdfrewrtet"
},
{
point: "77.606706,12.967060",
distance: "1.41",
id: 5686,
ph: "1234",
name: "zxcvb",
LS: " asdfrewrtet"
},
]