簡體   English   中英

從經緯度獲取地址

[英]Get address from latitude and longitude

我正在嘗試從android中的經度和緯度獲取位置地址。 我的代碼如下。

public String getAddress(double latitude, double longitude) {
        Geocoder myLocation = new Geocoder(this, Locale.getDefault());
        List<Address> myList = null;
        try {
            myList = myLocation.getFromLocation(latitude,longitude,1);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String address = myList.get(0).getAddressLine(0);
        return address;
    }

但是以某種方式我在myList = myLocation.getFromLocation(latitude,longitude,1);處獲得空返回值myList = myLocation.getFromLocation(latitude,longitude,1); 有什么事嗎

最好,

這很常見。如果服務器未返回任何值或過載,則將為Null。

我遇到了類似的問題,並在以下帖子中找到了解決方案。

http://girishbhutiya.blogspot.in/2010/04/reverse-geocoding-in-android-2-2-froyo-api-level-greater-than-8.html

你可以嘗試谷歌儲備,地理編碼 ,如果你想找到的地方,嘗試谷歌的地方

試試看,下面的代碼。

try {
                    // Get the location manager
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                        Criteria criteria = new Criteria();
                        bestProvider = locationManager.getBestProvider(criteria, false);
                        Location location = locationManager
                                .getLastKnownLocation(bestProvider);
                        double lat = location.getLatitude();
                        double lon = location.getLongitude();
                        Geocoder gc = new Geocoder(this);
                        List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
                        Address ad = lstAdd.get(0);
                        String str = ad.getAddressLine(0);
                    Toast tst = Toast.makeText(this, "Your current location is " + str,
                            Toast.LENGTH_LONG);
                    tst.show();
    } catch (Exception e) {
                    Toast tst = Toast.makeText(this,"Please check your gps settings",
                            Toast.LENGTH_LONG);
                    tst.show();
    }

但是,當然,請嘗試使用實際設備。

您可以通過google api使用,例如通過參數插入您的經度和緯度,然后將請求發送到服務器,並通過地址向服務器發送響應。

公共靜態字符串getAddressUrl(Context context){字符串responseText = null;

  /*String latitude="38.89";
  String longitude ="-77.03";*/
  Log.v(TAG , "Latitude is: " + latitude + "Longitude is:"+ longitude);
  StringBuilder sbuilder=new StringBuilder();
      String googleurl="http://maps.google.com/maps/geo?"
  sbuilder.append(googleurl);

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

 Log.v(TAG, "url is: "+url); 
  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;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM