【发布时间】:2016-09-11 23:27:54
【问题描述】:
我正在尝试从 json url 获取我附近存在的地方,但我只能在获取我的位置后获取这些地址,因为这样我才能获取最近的地方。 这样我就调用了获取onLocationChanged内的经纬度的方法。
GoogleMap mGoogleMap;
MapView mapFrag;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
public void onLocationChanged(Location location)
{
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
loadJson(location.getLatitude(),location.getLongitude());
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
问题在于我尝试使用 RequestQueue 和 StringRequest 获取 Json 数组的方式,从未达到内部类 Response.Listener 中的 onResonse 方法。 正是在这个方法中,我向地图添加了标记。
private void loadJson(double lat, double longit){
lat = -22.950491;
longit= -43.181609;
// Store values at the time of the login attempt.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://MyJsonURL";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
List<Places> arrayList = new ArrayList<Places>();
JSONArray jsonArray = new JSONArray(response);
for(int i = 0; i< jsonArray.length();i++){
if ((jsonArray.getJSONObject(i).optString("latitude") != "") && (jsonArray.getJSONObject(i).optString("longitude") != "")){
Places places = new Places();
places.setLatitude(jsonArray.getJSONObject(i).getDouble("latitude"));
places.setLongitude(jsonArray.getJSONObject(i).getDouble("longitude"));
places.setNome(jsonArray.getJSONObject(i).getString("nome"));
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(places.getLatitude(),escolasMaps.getLongitude())).title(places.getNome()));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String err = (error.getMessage()==null)?"SD Card failed":error.getMessage();
Log.e("sdcard-err2:",err);
}
});
queue.add(stringRequest);
queue.start();
}
我想解决的另一个问题是如何在标记标题的条目中输入一个值,所以当我点击它时,我将发送值而不是条目。
【问题讨论】:
-
我认为
http://MyJsonURL不会响应您的请求。我也不明白为什么 logcat 说它试图从 SD 中获取一些东西。如果您想获得答案,请停止复制粘贴代码的问题
标签: android json google-maps