添加标记谷歌Android地图异步任务 [英] Add marker to android google map with async task

查看:147
本文介绍了添加标记谷歌Android地图异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图地图标记添加到谷歌地图在android系统异步任务。我成立了自己的地图,并调用异步任务与此:

 公共类BreweryMap扩展ActionbarMenu {    BeerDataê;
    串beerID;
    GoogleMap的地图;    //从包啤酒细节
    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_brewerymap);
        //获取数据的啤酒
        意向意图= getIntent();
        捆绑额外= intent.getExtras();
        串breweryID = extras.getString(breweryID);        地图=((MapFragment)getFragmentManager()。findFragmentById(R.id.map))
                .getMap();        // TODO:获取啤酒厂LATT长,并添加标记
        //构造URL
        字符串URL =htt​​p://api.brewerydb.com/v2/brewery/\"+ breweryID +键= myKeyformat = JSON和放大器; withLocations = Y;        Log.d(地图,URL);
        //异步任务来获得啤酒口味标签百分比
        新AddBreweryMapMarkerJSON(这一点,图).execute(URL);

当asycn任务叫我分析了很久,并LATT并试图标记添加到我的地图JSON返回。我检索从JSON,这是我从我的日志认识一个漫长而LATT价值。标记只是没有被放置在地图上。

 公共类AddBreweryMapMarkerJSON扩展的AsyncTask<弦乐,太虚,字符串> {    上下文℃;
    私人ProgressDialog对话框;
    GoogleMap的mapIn;    公共AddBreweryMapMarkerJSON(上下文的背景下,GoogleMap的地图)
    {
        C =背景;
        mapIn =图;
        对话=新ProgressDialog(C);
    }    @覆盖
    保护字符串doInBackground(字符串...为arg0){
        // TODO自动生成方法存根
        返回readJSONFeed(为arg0 [0]);
    }    在preExecute保护无效(){
        Dialog.setMessage(获取啤酒厂信息);        Dialog.setTitle(加载);
        Dialog.setCancelable(假);
        Dialog.show();
    }    @覆盖
    保护无效onPostExecute(字符串结果){        尝试{            Log.d(中试,地图,);
            JSONObject的O =新的JSONObject(结果);            Log.d(啤酒,结果);
            字符串的经度= getLong之(O);
            字符串LATT = getLatt(O);            双longDouble = Double.parseDouble(经度);
            双lattDouble = Double.parseDouble(LATT);            //添加标记
            mapIn.addMarker(新的MarkerOptions()
                    .POSITION(新经纬度(longDouble,lattDouble))
                    .title伪(的Hello world));        }
        赶上(例外五){        }        Dialog.dismiss();    }    公共字符串的getName(JSONObject的JSON){
        串持有人;        尝试{
            持有人= json.getJSONObject(数据)的getString(名称)。        }赶上(JSONException E){
            持有人=N / A;
        }        回到持有人;    }    公共字符串调用getIcon(JSONObject的JSON){
        串持有人;        尝试{
            持有人= json.getJSONObject(数据)getJSONObject(图像)的getString(大)。        }赶上(JSONException E){
            持有人=N / A;
        }        回到持有人;    }    公共字符串getDescription(JSONObject的JSON){
        串持有人;        尝试{
            持有人= json.getJSONObject(数据)的getString(说明)。        }赶上(JSONException E){
            持有人=N / A;
        }        回到持有人;    }
    公共字符串得到年(JSONObject的JSON){
        串持有人;        尝试{
            持有人= json.getJSONObject(数据)的getString(建立);        }赶上(JSONException E){
            持有人=N / A;
        }        回到持有人;    }    公共字符串getLatt(JSONObject的JSON){
        串持有人;        尝试{
            持有人= json.getJSONObject(数据)getJSONArray(位置)getJSONObject(0).getString(纬度)。;        }赶上(JSONException E){
            支架=空;
        }        回到持有人;    }    公共字符串getLong之(JSONObject的JSON){
        串持有人;        尝试{
            持有人= json.getJSONObject(数据)getJSONArray(位置)getJSONObject(0).getString(经度)。;        }赶上(JSONException E){
            支架=空;
        }        回到持有人;    }    公共字符串readJSONFeed(字符串URL){
        StringBuilder的StringBuilder的=新的StringBuilder();
        HttpClient的HttpClient的=新DefaultHttpClient();
        HTTPGET HTTPGET =新HTTPGET(URL);
        尝试{
            HTT presponse响应= httpClient.execute(HTTPGET);
            状态行状态行= response.getStatusLine();
            INT状态code = statusLine.getStatus code();
            如果(状态code == 200){
                HttpEntity实体= response.getEntity();
                为InputStream的InputStream = entity.getContent();
                读者的BufferedReader =新的BufferedReader(
                        新的InputStreamReader(InputStream的));
                串线;
                而((行= reader.readLine())!= NULL){
                    stringBuilder.append(线);
                }
                inputStream.close();
            }其他{
                Log.d(JSON,无法下载文件);
            }
        }赶上(例外五){
            Log.d(readJSONFeed,e.getLocalizedMessage());
        }
        返回stringBuilder.toString();
    }}


解决方案

这里的问题似乎是,它不喜欢创造一个经纬度内添加标记。通过改变这样的:

  //添加标记
            mapIn.addMarker(新的MarkerOptions()
                    .POSITION(新经纬度(longDouble,lattDouble))
                    .title伪(的Hello world));

此,固定标记没有被添加:

 双longDouble = Double.parseDouble(经度);
            双lattDouble = Double.parseDouble(LATT);            经纬度positionOne =新的经纬度(lattDouble,longDouble);            //添加标记
            mapIn.addMarker(新的MarkerOptions()
                    .POSITION(positionOne)
                    .title伪(名));

I have an async task which I am trying to add map markers to a google map in android. I set up my map and call the async task with this:

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

when the asycn task is called I parse the json returned for a long and latt and attempt to add the marker to my map. I am retrieving a long and latt value from the json, which I know from my logs. The marker just is not being placed on the map.

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

}

解决方案

The issue here seemed to be that it did not like creating a LatLng inside adding the marker. By changing this:

//add marker
            mapIn.addMarker(new MarkerOptions()
                    .position(new LatLng(longDouble, lattDouble))
                    .title("Hello world"));

to this, fixed the marker not being added:

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

这篇关于添加标记谷歌Android地图异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆