我有一个异步任务,我试图将地图标记添加到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,并解析并尝试将标记添加到我的地图中。我从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();
}
}
答案 0 :(得分:0)
尝试将处理映射的代码放在处理程序中,然后从AsyncTask向处理程序发送消息。通常,必须在主线程中进行接口修改,而AsyncTask显然在侧线程中运行。
答案 1 :(得分:0)
请尝试将Google地图的相机位置设置为特定位置(如
)CameraPosition cameraPosition = new CameraPosition.Builder()。target(latLng) .zoom(12)//设置缩放 .tilt(30)//将相机的倾斜度设置为30度 。建立(); //从构建器创建CameraPosition // .bearing(90)//将摄像机的方向设置为向东 gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
答案 2 :(得分:0)
从你的JSON字符串中获取Lat / Lng点然后从你的JSON数组创建一个'LatLng点列表
ArrayList<LatLng> points = new ArrayList<LatLng>();
循环遍历字符串并添加到列表
points.add(new LatLng(lat,lng));
返回doInBackground
return points;
然后在onPostExecute
循环列表中并绘制每个
public void onPostExecute(ArrayList points){
for(LatLng point : points){
//plot the point to the map
}
}
当然,您在申报课程时也必须更改退货类型
AsyncTask<String, Void, ArrayList<LatLng>>
答案 3 :(得分:0)
这里的问题似乎是它不喜欢在添加标记的内部创建LatLng。通过改变这个:
//add marker
mapIn.addMarker(new MarkerOptions()
.position(new LatLng(longDouble, lattDouble))
.title("Hello world"));
,修复了未添加的标记:
double longDouble = Double.parseDouble(longitude);
double lattDouble = Double.parseDouble(latt);
LatLng positionOne = new LatLng(lattDouble, longDouble);
//add marker
mapIn.addMarker(new MarkerOptions()
.position(positionOne)
.title(name));