如何通过从JSON中提取数据来显示Google Map多个标记
下面是我的Java代码,我想解析我的JSON并添加Google Map标记:
package com.rhsp.locator;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
import com.rhsp.locator.R;
public class NearbyActivity extends Activity {
public ArrayList<Facilities> fList;
// FacilitiesAdapter fadapter;
private GoogleMap map;
private GoogleMap maps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby);
setTitle("Nearby Facilities");
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
LatLng HFLoc = new LatLng(-1.8691541, 30.0058391);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HFLoc, 8));
map.addMarker(new MarkerOptions()
.title("Here")
.snippet("Here")
.position(HFLoc));
fList = new ArrayList<Facilities>();
new JSONAsyncTask().execute("http://bclrwanda.com/health/allfa.php");
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(NearbyActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting to server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("allcenters");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Facilities centres = new Facilities();
centres.setFacilityName(object.getString("hcname"));
centres.setCatName(object.getString("catname"));
centres.setSector(object.getString("sector"));
centres.setLatitude(object.getDouble("latitude"));
centres.setLongitude(object.getDouble("longitude"));
// Display values
double lat = object.getDouble("latitude");
double lon = object.getDouble("longitude");
System.out.println("Lat: " + lat + " Lon: " + lon);
fList.add(centres);
}
return true;
}
//------------------>> b
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
//fadapter.notifyDataSetChanged();
try{
for(int i=0; i<fList.size(); i++){
System.out.println("Latitude: " + fList.get(i));
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("This went wrong: " + e.getMessage());
}
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
以下是我想要用来添加Google地图标记的JSON:
{"allcenters":[
{
"hcid":"13","hcname":"Kacyiru Cs",
"sector":"Kacyiru",
"longitude":"30.0752",
"latitude":"-1.9331",
"catname":"HC"
},
{
"hcid":"11",
"hcname":"Gashora Cs",
"sector":"Gashora",
"longitude":"1.3263263",
"latitude":"30.15498645",
"catname":"HC"
},
{
"hcid":"12",
"hcname":"Juru Cs",
"sector":"Juru",
"longitude":"1.3263263",
"latitude":"30.15498645",
"catname":"HO"
},
{
"hcid":"14",
"hcname":"Kacyiru Police Hospital",
"sector":"Kacyiru",
"longitude":"30.075489",
"latitude":"-1.932984",
"catname":"HO"
},
{
"hcid":"15",
"hcname":"Kabusunzu Cs",
"sector":"Nyakabanda",
"longitude":"30.0519",
"latitude":"-1.963",
"catname":"HC"
},
{
"hcid":"16",
"hcname":"Kimironko Cs",
"sector":"Kimironko",
"longitude":"30.1261",
"latitude":"-1.9514",
"catname":"HC"
},
{
"hcid":"17",
"hcname":"Caraes Ndera",
"sector":"Ndera",
"longitude":"30.169",
"latitude":"-1.9547",
"catname":"HO"
},
{
"hcid":"18",
"hcname":"Avega Kigali Cs",
"sector":"Remera",
"longitude":"30.1068",
"latitude":"-1.9585",
"catname":"HC"
},
{
"hcid":"19",
"hcname":"Rutunga Hp",
"sector":"Rutunga",
"longitude":"30.1068",
"latitude":"-1.9585",
"catname":"HO"
},
{
"hcid":"20",
"hcname":"Butaro Hp",
"sector":"Butaro",
"longitude":"29.8366",
"latitude":"-1.4089",
"catname":"HO"
},
{
"hcid":"21",
"hcname":"Gikonko Cs",
"sector":"Gikonko",
"longitude":"29.8552",
"latitude":"-2.484",
"catname":"HC"
},
{
"hcid":"22",
"hcname":"Pharmacy Continental",
"sector":"Kibaza",
"longitude":"30.085321",
"latitude":"-1.934726",
"catname":"HO"
},
{
"hcid":"23",
"hcname":"King Faycal Hospital",
"sector":"Kacyiru",
"longitude":"30.0951898",
"latitude":"-1.9439559",
"catname":"HO"
}
]}
答案 0 :(得分:0)
在地图准备好后添加标记。这可以通过使用onMapReady()覆盖方法来实现。
@Override
public void onMapReady(GoogleMap map) {
//use a for loop to process your json data here
map.addMarker(new MarkerOptions()
.position(new LatLng(yourlatitudes, yourlongitudes))
.title("Title for marker"));
}