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());
  }
 
这样就可以得到了经纬度

相关内容

    暂无相关文章