如何使用Google MAP API通过lat,lng找到最近的城市

时间:2017-04-19 10:35:15

标签: php google-maps google-maps-api-3 geo

我正在尝试使用Google Map API获取城市名称但未获得。 我现在有了使用Google API的Lat,Lng,我希望在10KM的距离内获得城市名称。

我使用以下网址获取LatLnghttps://maps.googleapis.com/maps/api/geocode/json?address=Coburg,Austrelia&key= “”

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=".$lat.",".$long."&radius=10000&type=administrative_area_level_2&key=""

1 个答案:

答案 0 :(得分:0)

You can try this
ProgressDialog dialog=null;
   @Override
    public void onClick(View v) {
            dialog = ProgressDialog.show(context, "", "Please! Wait...", true);
            GetCurrentAddress result = new GetCurrentAddress();
            result.execute("");
        }



    // getting address task
    private class GetCurrentAddress extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            String responseText = null;
            String googleurl = "http://maps.google.com/maps/geo?";
            String latitude="38.89";
            String longitude ="-77.03";
      StringBuilder sbuilder=new StringBuilder();
      sbuilder.append(googleurl);

      sbuilder.append("q="+latitude+","+longitude);
      sbuilder.append("&amp;output=responseText&amp;sensor=true");
      String url=sbuilder.toString();

      try { 
          DefaultHttpClient httpclient=new DefaultHttpClient(); 
          HttpPost httppost=new HttpPost(url); 
          HttpResponse httpresponse=httpclient.execute(httppost); 
          HttpEntity httpentity=httpresponse.getEntity(); 
          InputStream is=httpentity.getContent();
          BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
          StringBuilder sb=new StringBuilder();
          String line=null;
        while((line=reader.readLine())!=null)
         { 
          sb.append(line+"\n");

         } 
        is.close(); 
        responseText=sb.toString();
      } 
      catch(ClientProtocolException e) 
      { 
          e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
          e.printStackTrace();
      }
      return responseText;
      }

        @Override
        protected void onPostExecute(String resultString) {
            dialog.dismiss();

            String response= resultString;

            if (responseText != null) {
                Log.v(TAG, "json response is:" + response);
                try {
                    jObj = new JSONObject(response);
                    jsonarray = jObj.getJSONArray("Placemark");
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject innerJsonobject = jsonarray.getJSONObject(i);
                        String jsonaddress = innerJsonobject
                                .getString("address");


                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }