使用异步任务向android google地图添加标记

使用异步任务向android google地图添加标记,android,json,google-maps,Android,Json,Google Maps,我有一个异步任务,我正在尝试将地图标记添加到android中的google地图。我设置映射并使用以下命令调用异步任务: public class BreweryMap extends ActionbarMenu { BeerData e; String beerID; GoogleMap map; //get beer details from bundle protected void onCreate(Bundle savedInstanceSta

我有一个异步任务,我正在尝试将地图标记添加到android中的google地图。我设置映射并使用以下命令调用异步任务:

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();
    }

}
公共类AddBreweryMapMarkerJSON扩展异步任务{
上下文c;
私人对话;
谷歌地图;
公共AddBreweryMapMarkerJSON(上下文上下文,谷歌地图)
{
c=上下文;
mapIn=map;
Dialog=新进度对话框(c);
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
返回readJSONFeed(arg0[0]);
}
受保护的void onPreExecute(){
setMessage(“获取啤酒厂信息”);
对话框.setTitle(“加载”);
对话框。可设置可取消(false);
Dialog.show();
}
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
Log.d(“map”、“in try”);
JSONObject o=新的JSONObject(结果);
日志d(“酿酒厂”,结果);
字符串经度=getLong(o);
字符串latt=getLatt(o);
double longDouble=double.parseDouble(经度);
双lattDouble=double.parseDouble(latt);
//添加标记
mapIn.addMarker(新标记选项()
.位置(新板条(长双板条、板条双板条))
.title(“Hello world”);
}
捕获(例外e){
}
Dialog.dismise();
}
公共字符串getName(JSONObject json){
绳夹;
试一试{
holder=json.getJSONObject(“数据”).getString(“名称”);
}捕获(JSONException e){
持有人=“不适用”;
}
报税表持有人;
}
公共字符串getIcon(JSONObject json){
绳夹;
试一试{
holder=json.getJSONObject(“数据”).getJSONObject(“图像”).getString(“大”);
}捕获(JSONException e){
持有人=“不适用”;
}
报税表持有人;
}
公共字符串getDescription(JSONObject json){
绳夹;
试一试{
holder=json.getJSONObject(“数据”).getString(“说明”);
}捕获(JSONException e){
持有人=“不适用”;
}
报税表持有人;
}
公共字符串getYear(JSONObject json){
绳夹;
试一试{
holder=json.getJSONObject(“数据”).getString(“已建立”);
}捕获(JSONException e){
持有人=“不适用”;
}
报税表持有人;
}
公共字符串getLatt(JSONObject json){
绳夹;
试一试{
holder=json.getJSONObject(“数据”).getJSONArray(“位置”).getJSONObject(0).getString(“纬度”);
}捕获(JSONException e){
holder=“null”;
}
报税表持有人;
}
公共字符串getLong(JSONObject json){
绳夹;
试一试{
holder=json.getJSONObject(“数据”).getJSONArray(“位置”).getJSONObject(0).getString(“经度”);
}捕获(JSONException e){
holder=“null”;
}
报税表持有人;
}
公共字符串readJSONFeed(字符串URL){
StringBuilder StringBuilder=新的StringBuilder();
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(URL);
试一试{
HttpResponse response=httpClient.execute(httpGet);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
InputStream InputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(inputStream));
弦线;
而((line=reader.readLine())!=null){
stringBuilder.append(行);
}
inputStream.close();
}否则{
Log.d(“JSON”,“未能下载文件”);
}
}捕获(例外e){
d(“readJSONFeed”,例如getLocalizedMessage());
}
返回stringBuilder.toString();
}
}

尝试将进行映射修改的代码放入处理程序中,然后从异步任务向处理程序发送消息。通常,接口修改必须在主线程中进行,而AsyncTask显然在副线程中运行

请尝试将您在谷歌地图上的相机位置设置为特定位置的动画,如

CameraPosition CameraPosition=新建CameraPosition.Builder().target(latLng) .zoom(12)//设置缩放 .tilt(30)//将相机的倾斜设置为30度 .build();//从生成器中创建摄影机位置 //.bearing(90)//将相机的方向设置为东
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))

从JSON字符串中获取Lat/Lng点,然后从JSON数组中创建一个“LatLng点”列表

ArrayList<LatLng> points = new ArrayList<LatLng>();
返回
doInBackground中的列表

return points;
然后在
onPostExecute
中循环浏览列表并绘制每个列表

public void onPostExecute(ArrayList points){
    for(LatLng point : points){
        //plot the point to the map
    }
}
当然,在声明类时还必须更改返回类型

AsyncTask<String, Void, ArrayList<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));

嗯,在我调查之前,我从来没有冒险进入过处理程序。谢谢@adamp提供的信息,我不知道!我不知道你的拿铁是什么格式的,我有两个双人的,一个是拿铁的,一个是长的
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));