您的位置:首页 > 移动开发 > Android开发

Android高德地图输入地址利用正地理编码获取经纬度

2017-05-26 16:05 781 查看
在项目中需要完成一个功能,选择某一个城市的名字,结束当前页面,地图页面移动到选择的城市,这么一个功能,因为项目中用的是高德地图,所以下面先附上图片。



现在就来实现上面的功能。

第一步 输入想要搜索的名字,比如北海市,然后发起正地理编码搜索。

GeocodeSearch("北海市");
//发起正地理编码搜索
public void GeocodeSearch(String city) {
//构造 GeocodeSearch 对象,并设置监听。
geocodeSearch = new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(this);
//通过GeocodeQuery设置查询参数,调用getFromLocationNameAsyn(GeocodeQuery geocodeQuery) 方法发起请求。
//address表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode都ok
GeocodeQuery query = new GeocodeQuery(city, city);
geocodeSearch.getFromLocationNameAsyn(query);
}


第二步 通过正地理编码获取北海市的经纬度,在正地理编码的监听方法中获取。

@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

}

//正地理编码
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

if (i == AMapException.CODE_AMAP_SUCCESS) {
if (geocodeResult != null && geocodeResult.getGeocodeAddressList() != null
&& geocodeResult.getGeocodeAddressList().size() > 0) {
GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0);
addressName = "经纬度值:" + address.getLatLonPoint() + "\n位置描述:"
+ address.getFormatAddress();
//获取到的经纬度
LatLonPoint latLongPoint = address.getLatLonPoint();
Lat = (float)latLongPoint.getLatitude();
Lon = (float)latLongPoint.getLongitude();
Log.d("111",Double.toString(Lat));

//然后把经纬度传递给地图界面
intent = new Intent();
Bundle bundle = new Bundle();
bundle.putDouble("Lat",Lat);
bundle.putDouble("Lng",Lon);
intent.putExtra("LAT",bundle);
CityChangeActivity.this.setResult(8,intent);
CityChangeActivity.this.finish();
}
}
}


第三步 得到经纬度之后,把地图移动到要移动的位置。

Bundle bundle = new Bundle();
bundle = data.getBundleExtra("LAT");
double Lat = bundle.getDouble("Lat");
double Lng = bundle.getDouble("Lng");
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng
(Lat, Lng), 13.0f));
}


这样就完成了上述的那个功能,也可以只单独的获取经纬度,希望对别人有用,勿喷。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐