我想创建多个具有经度和纬度的标记,所以我在Google Maps活动中以某种方式编写了这段代码,代码没有运行低谷,因为我得到了第一个logcat输出“ jsonParse:现在在json Parse中” 。为什么它停止在我的@override方法上。
因此,我编写了一个测试项目,将所有内容打印到TextView中,并且代码一直运行。我不明白为什么它不适用于我的GoogleMaps andaroid项目?测试项目运行正常
这是Maps类的主要活动: 测试版本是相同的代码,但不能在android studio GoogleMap项目中运行。唯一的区别是,在测试项目中,我在on create上将其称为。</ p>
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private RequestQueue mQueue;
private static final String TAG = "xd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
jsonParse();
mMap = googleMap;
/* // Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
}
private void jsonParse() {
String url = "https://api.myjson.com/bins/kp9wz";
Log.d(TAG, "jsonParse: is now in jsonParse ");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: is before try");
try {
JSONArray jsonArray = response.getJSONArray("addresses");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject address = jsonArray.getJSONObject(i);
String firstName = address.getString("name");
Double latitude = Double.parseDouble(address.getString("latitude"));
Double longitude = Double.parseDouble(address.getString("longitude"));
LatLng sydney = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
}
Log.d(TAG, "onResponse: is after for loop");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
}
}
答案 0 :(得分:0)
您已创建请求,但尚未将请求添加到请求队列中。