设为首页 加入收藏

TOP

Android百度地图开发之通过地址获得经纬度
2014-11-24 07:37:42 来源: 作者: 【 】 浏览:0
Tags:Android 百度 地图 开发 通过 地址 获得 纬度

在百度地图开发的时候,我们经常会通过地址去得到当前地址的经纬度,那么我们怎么得到呢?

方法一、

public GeoPoint getGeoPointBystr(String str) {
GeoPoint gpGeoPoint = null;
if (str!=null) {
Geocoder gc = new Geocoder(MyMapActivity.this,Locale.CHINA);
List

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());
}

这样就可以得到了经纬度


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇struts2下如何实现伪静态 下一篇Java文件的 In 与 Out 的相对性

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)