【发布时间】:2017-12-18 23:35:15
【问题描述】:
我正在尝试从 json 在 google map v2 上添加多个标记 这是我迄今为止尝试过的,但无法在地图上找到单个标记。
我觉得我在添加标记时犯了错误, 我应该添加另一种添加标记的方法还是应该准备好地图 或者我应该使用两个单独的活动,一个用于谷歌地图,另一个用于 JSON,如果您可以在这里提出一些更正建议。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
HashMap<String, Double> resultp;
private String TAG = MapsActivity.class.getSimpleName();
private ProgressDialog pDialog;
private static String url = "https://api.myjson.com/bins/1an69r";
ArrayList<HashMap<String, String>> placeList;
ArrayList<HashMap<String, Double>> Coordinate;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
new GetPlace().execute();
placeList = new ArrayList<>();
Coordinate = new ArrayList<>();
resultp = new HashMap<>();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
private class GetPlace extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MapsActivity.this);
pDialog.setMessage("wait bro");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray places = jsonObj.getJSONArray("places");
// looping through All places
for (int i = 0; i < places.length(); i++) {
JSONObject c = places.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String city = c.getString("city");
String needle = c.getString("needle");
Double lat = c.getDouble("lat");
Double lng = c.getDouble("lng");
String rating = c.getString("rating");
// tmp hash map for single contact
HashMap<String, String> place = new HashMap<>();
// adding each child node to HashMap key => value
place.put("id", id);
place.put("name", name);
place.put("city", city);
place.put("needle", needle );
place.put("rating",rating);
placeList.add(place);
//adding to new hashmap
HashMap<String, Double> lace = new HashMap<>();
lace.put("lat",lat);
lace.put("lng",lng);
// adding contact to place list
Coordinate.add(lace);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
**@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for( int i=0;i<Coordinate.size();i++)
{
resultp = Coordinate.get(i);
Double lAT = resultp.get("lat");
Double lNG = resultp.get("lng");
//Plot latitude and longitude in the map
LatLng lk = new LatLng(lAT,lNG);
googleMap.addMarker(new MarkerOptions().position(new LatLng(lAT, lNG)));
Log.e("PlaceLL",lAT+" "+lNG);
}**
}
}
【问题讨论】:
-
您是否在您的
onMapReady()方法中确认Coordinate不为空?我的第一个猜测是没有任何数据可以添加标记。顺便说一句,现在是学习使用调试器的好时机。 -
您是否尝试过在
onMapReady()上调用new GetPlace().execute();,以便确定地图在尝试设置标记之前已初始化?然后在OnPostExecute()上有类似MainActivity.this.addMarkers() -
onMapReady在您的 Web 服务调用之前调用,这会使坐标为空。尝试在你的异步任务之后初始化你的地图onPostExecute
标签: android json google-maps dictionary marker