【问题标题】:Add add to google map and then search on it添加添加到谷歌地图,然后搜索它
【发布时间】:2014-12-15 13:48:24
【问题描述】:

我是谷歌地图和谷歌地图 api 的新手 我想知道我的 android 应用程序如何将数据添加到谷歌地图“像位置” 然后其他用户可以在最近的搜索请求中获取此位置
换句话说

我的应用程序将允许人们将他们的位置添加到我的谷歌地图,并允许用户按位置搜索到其他用户添加的最近位置,而不仅仅是谷歌地方库 因此应用程序可以获取包含用户位置附近最近位置的 json 文件

我如何使用 Google API 做到这一点?

【问题讨论】:

  • 如果你想在你的应用中显示谷歌地图视图,你需要使用谷歌 API
  • 我不想从谷歌地图中获取最近的地点列表并在我的应用程序中使用它

标签: android google-maps google-maps-api-3


【解决方案1】:

可能会有所帮助

// Fetches all places from GooglePlaces AutoComplete Web Service
private class PlacesTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... place) {
        // For storing data from web service
        String data = "";

        // Obtain browser key from https://code.google.com/apis/console
        String key = "key=key";

        String input="";

        try {
            input = "input=" + URLEncoder.encode(place[0], "utf-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }       


        // place type to be searched
        String types = "types=geocode";

        // Sensor enabled
        String sensor = "sensor=false";         

        // Building the parameters to the web service
        String parameters = input+"&"+types+"&"+sensor+"&"+key;

        // Output format
        String output = "json";

        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;

        try{
            // Fetching the data from web service in background
            data = downloadUrl(url);
        }catch(Exception e){
            Log.d("Background Task",e.toString());
        }
        return data;        
    }

    @Override
    protected void onPostExecute(String result) {           
        super.onPostExecute(result);

        // Creating ParserTask
        parserTask = new ParserTask();

        // Starting Parsing the JSON string returned by Web Service
        parserTask.execute(result);
    }       
}


/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{

    JSONObject jObject;

    @Override
    protected List<HashMap<String, String>> doInBackground(String... jsonData) {            

        List<HashMap<String, String>> places = null;

        PlaceJSONParser placeJsonParser = new PlaceJSONParser();

        try{
            jObject = new JSONObject(jsonData[0]);

            // Getting the parsed data as a List construct
            places = placeJsonParser.parse(jObject);

        }catch(Exception e){
            Log.d("Exception",e.toString());
        }
        return places;
    }

    @Override
    protected void onPostExecute(List<HashMap<String, String>> result) {            

            String[] from = new String[] { "description"};
            int[] to = new int[] { android.R.id.text1 };

            // Creating a SimpleAdapter for the AutoCompleteTextView            
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);             

            // Setting the adapter
            atvPlaces.setAdapter(adapter);
    }           
}    

输出

【讨论】:

  • 很好 :) 但我需要保存我的应用用户位置,然后检索特定地址“用户位置”周围最近的列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2012-06-14
  • 2015-12-12
  • 1970-01-01
相关资源
最近更新 更多