當前位置:編程學習大全網 - 源碼下載 - 百度地圖android中怎麽通過地址獲取經緯度

百度地圖android中怎麽通過地址獲取經緯度

在百度地圖開發的時候,我們經常會通過地址去得到當前地址的經緯度,方法如下:

方法壹、

public GeoPoint getGeoPointBystr(String str) {

GeoPoint gpGeoPoint = null;

if (str!=null) {

Geocoder gc = new Geocoder(MyMapActivity.this,Locale.CHINA);

List<Address> addressList = null;

try {

addressList = gc.getFromLocationName(str, 1);

if (!addressList.isEmpty()) {

Address address_temp = addressList.get(0);

//計算經緯度

double Latitude=address_temp.getLatitude()*1E6;

double Longitude=address_temp.getLongitude()*1E6;

System.out.println("經度:"+Latitude);

System.out.println("緯度:"+Longitude);

//生產GeoPoint

gpGeoPoint = new GeoPoint((int)Latitude, (int)Longitude);

}

} catch (IOException e) {

e.printStackTrace();

}

}

return gpGeoPoint;

}

此方法只需傳入壹個地址即可(當然,這裏應該說是壹個合法的地址)

此方法得到壹個GeoPoint對象,通過GeoPoint對象.getLatitude()/getLongitude()就可以得到對應的經緯度

但是值得註意的是,以上方法存在API版本問題,2.2版本的不可以用

方法二、(個人比較推薦這種方法)

mkSearch.geocode("詳細地址", "城市");

這裏的詳細地址可以通過MKSuggestionInfo對象.key得到,而城市也可以根據MKSuggestionInfo對象.city得到

調用以上方法後,就會在執行實現MKSearchListener接口類中的以下方法

public void onGetAddrResult(MKAddrInfo info, int error) {

// TODO Auto-generated method stub

System.out.println("經緯度:"+info.geoPt.getLatitudeE6()+" "+info.geoPt.getLongitudeE6());

}

這樣就可以得到了經緯度

  • 上一篇:小程序這麽火,怎麽利用小程序賺錢
  • 下一篇:UI設計難學嗎?
  • copyright 2024編程學習大全網