所以在解决了我的问题Converting JSONObject to JSONArray后,我能够从JSON文件中获取Lat,Long。但是,我似乎无法让标记显示出来。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
JSONParser jsonParser = new JSONParser();
private GoogleMap mMap;
private GoogleMap nMap;
private static final LatLng Tampines = new LatLng(1.3525,103.9446);
List<TaxiCoordinates> myCoordinates = new ArrayList<TaxiCoordinates>();
private ProgressDialog pDialog;
MapsActivity map;
private static final String apiURL = "http://datamall2.mytransport.sg/ltaodataservice/TaxiAvailability/";
@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.
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
myCoordinates = new ArrayList<TaxiCoordinates>();
new GetCoordinates().execute();
}
/**
* 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) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Tampines, 10));
// 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));
}
class GetCoordinates extends AsyncTask<String, String, JSONArray> {
private ProgressDialog progressDialog;
@Override
protected JSONArray doInBackground(String... params)
{
try {
JSONArray json = jsonParser.makeHttpRequest(
apiURL, "GET");
if (json != null) {
Log.d("JSON result", json.toString());
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MapsActivity.this);
progressDialog.setMessage("Loading Taxis...");
progressDialog.show();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
if (jsonArray != null)
{
//loop
for(int i = 0; i < jsonArray.length(); i++)
{
try
{
JSONObject c = jsonArray.getJSONObject(i);
TaxiCoordinates temp = new TaxiCoordinates();
temp.setLatitude(c.getDouble("Latitude"));
temp.setLongitude(c.getDouble("Longitude"));
myCoordinates.add(temp);
if(progressDialog!=null && progressDialog.isShowing())
{
progressDialog.dismiss();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
addMarkers();
}
}
}
public void addMarkers()
{
for(TaxiCoordinates c: myCoordinates)
{
double tempLat = c.getLatitude();
double tempLong = c.getLongitude();
LatLng taxi = new LatLng(tempLat, tempLong);
mMap.addMarker(new MarkerOptions()
.position(taxi)
.title("Available"));
//MarkerOptions m = new MarkerOptions().title("Available").position(new LatLng(tempLat, tempLong));
//googleMap.addMarker(m);
}
}
}
顺便说一句,我尝试将addMarkers()
的代码放入正确的onMapReady()
,但仍然没有显示。
mMap.addMarker(new MarkerOptions().position(tempLat,tempLng).title(Available).snippet(details).icon(BitmapDescriptorFactory.fromResource(markericon))); //tried default ones too.
答案 0 :(得分:0)
没关系,我是个傻瓜。这样做我错过了一部分。
mMap.addMarker(new MarkerOptions().position(tempLat,tempLng).title(Available).snippet(details).icon( .fromResource(markericon))); //tried default ones too.