【发布时间】:2013-12-21 15:47:49
【问题描述】:
我有一个异步任务,我正在尝试将地图标记添加到 android 中的谷歌地图。我设置了我的地图并用这个调用异步任务:
public class BreweryMap extends ActionbarMenu {
BeerData e;
String beerID;
GoogleMap map;
//get beer details from bundle
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brewerymap);
//get beer data
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String breweryID = extras.getString("breweryID");
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
//todo: get brewery latt and long and add marker
//construct url
String url = "http://api.brewerydb.com/v2/brewery/"+ breweryID +"?key=myKeyformat=json&withLocations=y";
Log.d("map", url);
//async task to get beer taste tag percents
new AddBreweryMapMarkerJSON(this,map).execute(url);
当调用 asycn 任务时,我解析返回的 json 长和 latt 并尝试将标记添加到我的地图中。我正在从 json 中检索一个 long 和 latt 值,我从我的日志中知道。标记只是没有放置在地图上。
public class AddBreweryMapMarkerJSON extends AsyncTask<String, Void, String> {
Context c;
private ProgressDialog Dialog;
GoogleMap mapIn;
public AddBreweryMapMarkerJSON(Context context, GoogleMap map)
{
c = context;
mapIn = map;
Dialog = new ProgressDialog(c);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPreExecute() {
Dialog.setMessage("Getting brewery information");
Dialog.setTitle("Loading");
Dialog.setCancelable(false);
Dialog.show();
}
@Override
protected void onPostExecute(String result){
try{
Log.d("map", "in try");
JSONObject o = new JSONObject(result);
Log.d("brewery", result);
String longitude = getLong(o);
String latt = getLatt(o);
double longDouble = Double.parseDouble(longitude);
double lattDouble = Double.parseDouble(latt);
//add marker
mapIn.addMarker(new MarkerOptions()
.position(new LatLng(longDouble, lattDouble))
.title("Hello world"));
}
catch(Exception e){
}
Dialog.dismiss();
}
public String getName(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getString("name");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getIcon(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getJSONObject("images").getString("large");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getDescription(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getString("description");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getYear(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getString("established");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getLatt(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getJSONArray("locations").getJSONObject(0).getString("latitude");
} catch (JSONException e) {
holder = "null";
}
return holder;
}
public String getLong(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getJSONArray("locations").getJSONObject(0).getString("longitude");
} catch (JSONException e) {
holder = "null";
}
return holder;
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
【问题讨论】:
标签: android json google-maps