我想在谷歌地图活动中的谷歌地图上显示我的主要活动中填充了坐标的arraylist。我怎样才能以最简单的方式做到这一点? 我的for循环获取JSON数据:
for (int j = 0; j <= 10; j++) {
JSONObject marker = placemarks.getJSONObject(j);
JSONArray coordinates = marker.optJSONArray("coordinates");
double lat = coordinates.optDouble(0);
double lng = coordinates.optDouble(1);
latlongList.add(new LatLng(lat, lng));
}
我目前的谷歌地图活动:
public class CarLocation extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car2_go_location);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
MainActivity mainActivity = new MainActivity();
mMap = googleMap;
// Add a marker in Hamburg and move the camera
LatLng hamburg = new LatLng(53.551086, 9.993682);
mMap.addMarker(new MarkerOptions().position(hamburg).title("Marker in Hamburg"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(hamburg));
}
}
答案 0 :(得分:2)
我没有测试这段代码,但为了理解如何操作,你可以关注。
我在上面的评论中提到过,不是在活动之间发送整个列表(在你的情况下从MainActivity到CarLocation活动),最好在一个方法和使用中包装整个过程(获取json和显示标记)。
DisplayMarkersTask asynctask获取并显示标记。
public class CarLocation extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private List<Marker> markers = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car2_go_location);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Send parameter if needed
String param = ....
new DisplayMarkersTask(param).execute();
}
private static class DisplayMarkersTask extends AsyncTask<String, Void, ArrayList<LatLng>> {
private WeakReference<CarLocation> mActivityRef;
public DisplayMarkersTask(CarLocation activity){
mActivityRef = new WeakReference<>(activity);
}
@Override
protected ArrayList<LatLng> doInBackground(String... params) {
Take parameter if needed
String param = params[0];
....
Fetch your json here
ArrayList<LatLng> latlongList = new ArrayList<>();
for (int j = 0; j <= 10; j++) {
JSONObject marker = placemarks.getJSONObject(j);
JSONArray coordinates = marker.optJSONArray("coordinates");
double lat = coordinates.optDouble(0);
double lng = coordinates.optDouble(1);
latlongList.add(new LatLng(lat, lng));
}
return latlongList;
}
@Override
protected void onPostExecute(ArrayList<LatLng> list) {
CarLocation activity = mActivityRef.get();
if(activity == null){
return;
}
for (int j = 0; j <= list.size(); j++) {
LatLng position = list.get(j);
Marker marker = activity.mMap.addMarker(
new MarkerOptions().position(position));
activity.markers.add(marker);
}
}
}
}
答案 1 :(得分:0)