我是Android新手,我很難獲取Google地圖v2標記以顯示。如果我放入一個測試標記,並且其中有一些硬編碼值,它會很好地工作。但是,我的代碼似乎不工作。標記不會在Google地圖v2上顯示
我已經打了折扣,看到了值是正確的,並進入函數,但我無法讓它工作。
我也調試到物理設備。
下面的代碼
private void inflateGoogleMap(ArrayList<Geocache> cacheList){
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
for(int i=0; i<cacheList.size(); i++){
Log.d("CACHE_NAME", cacheList.get(i).getName());
Log.d("CACHE_LAT", Double.toString(cacheList.get(i).getLatitude()));
Log.d("CACHE_LONG", Double.toString(cacheList.get(i).getLongitude()));
Double cacheLat = cacheList.get(i).getLatitude();
Double cacheLong = cacheList.get(i).getLongitude();
LatLng pos = new LatLng(cacheLat, cacheLong);
map.addMarker(new MarkerOptions().position(pos));
//Log.d("CACHE_TYPE", json.getString("type"));
//Log.d("CACHE_REGION", json.getString("region"));
/*Marker marker = map.addMarker(new MarkerOptions()
.title(cacheList.get(i).getName())
.snippet(cacheList.get(i).getName())
.position(new LatLng(cacheList.get(i).getLatitude(), cacheList.get(i).getLongitude())));*/
}
}
我如何獲得JSON的列表
public void getNearbyGeocaches(double lat, double lon,int limit){
//String url = "http://test.opencaching.com/api/geocache?center=38.642%2c-94.754&limit=5&Authorization="+API_KEY;
String url = "http://test.opencaching.com/api/geocache?center="+lat+"%2c"+lon+"&limit="+limit+"&Authorization="+super.getApiKey();
super.get(url, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray response) {
// Pull out the first event on the public timeline
JSONObject json = null;
ArrayList<Geocache> geoList = new ArrayList<Geocache>();
//Log.d("JSON_LENGTH",Integer.toString(timeline.length()));
for(int i = 0; i<response.length(); i++)
{
try {
//Cast response para JSONObject
json = (JSONObject) response.get(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
Geocache cache = new Geocache();
//Log.d("CACHE_NAME", json.getString("name"));
//Log.d("CACHE_TYPE", json.getString("type"));
//Log.d("CACHE_REGION", json.getString("region"));
cache.setName(json.getString("name"));
cache.setDescription(json.getString("description"));
JSONObject location = json.getJSONObject("location");
cache.setLatitude(Double.parseDouble(location.getString("lon")));
cache.setLongitude(Double.parseDouble(location.getString("lat")));
//Log.d("CACHE_LAT", location.getString("latitude"));
//Log.d("CACHE_LONG", location.getString("longitude"));
//cache.setName(firstEvent.getString("name"));
geoList.add(cache);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
}
inflateGoogleMap(geoList);
// Do something with the response
}
});
}
感謝您的幫助!
你能得到你的緯度和經度值從JSON嗎? –
是的。它們正確地顯示在日誌上... –