百度地图之地址信息和坐标的转换
来源:互联网 发布:西班牙语离线翻译软件 编辑:程序博客网 时间:2024/06/05 08:09
在实际运用中,经常需要进行地理编码和地理反编码,即将地址信息转换成坐标和将坐标转换成地址信息,此demo就是用来展示如何进行地理编码搜索(用地址检索坐标)、反地理编码搜索(用坐标检索地址)以及展示如何使用ItemizedOverlay在地图上标注结果点,代码原型来自百度Demo,代码如下:
Activity:
package com.home;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.baidu.mapapi.map.ItemizedOverlay;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.OverlayItem;import com.baidu.mapapi.search.MKAddrInfo;import com.baidu.mapapi.search.MKBusLineResult;import com.baidu.mapapi.search.MKDrivingRouteResult;import com.baidu.mapapi.search.MKPoiResult;import com.baidu.mapapi.search.MKSearch;import com.baidu.mapapi.search.MKSearchListener;import com.baidu.mapapi.search.MKShareUrlResult;import com.baidu.mapapi.search.MKSuggestionResult;import com.baidu.mapapi.search.MKTransitRouteResult;import com.baidu.mapapi.search.MKWalkingRouteResult;import com.baidu.platform.comapi.basestruct.GeoPoint;public class GeoCoderActivity extends Activity implements OnClickListener {// UI相关private Button mBtnReverseGeoCode = null; // 将坐标反编码为地址private Button mBtnGeoCode = null; // 将地址编码为坐标private EditText lat = null;private EditText lon = null;private EditText editCity = null;private EditText editGeoCodeKey = null;// 地图相关private MapView mMapView = null; // 地图View// 搜索相关private MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);DemoApplication app = (DemoApplication) this.getApplication();setContentView(R.layout.geocoder);CharSequence titleLable = "地理编码功能";setTitle(titleLable);// 地图初始化mMapView = (MapView) findViewById(R.id.geocoder_bmapView);mMapView.getController().enableClick(true);mMapView.getController().setZoom(12);// UI初始化lat = (EditText) findViewById(R.id.geocoder_et_lat);lon = (EditText) findViewById(R.id.geocoder_et_lon);editCity = (EditText) findViewById(R.id.geocoder_et_city);editGeoCodeKey = (EditText) findViewById(R.id.geocoder_et_geocodekey);mBtnReverseGeoCode = (Button) findViewById(R.id.geocoder_btn_reversegeocode);mBtnGeoCode = (Button) findViewById(R.id.geocoder_btn_geocode);mBtnReverseGeoCode.setOnClickListener(this);mBtnGeoCode.setOnClickListener(this);// 初始化搜索模块,注册事件监听mSearch = new MKSearch();mSearch.init(app.mBMapManager, new MKSearchListener() {@Overridepublic void onGetPoiDetailSearchResult(int type, int error) {}public void onGetAddrResult(MKAddrInfo res, int error) {if (error != 0) {String str = String.format("错误号:%d", error);Toast.makeText(GeoCoderActivity.this, str,Toast.LENGTH_LONG).show();return;}// 地图移动到该点mMapView.getController().animateTo(res.geoPt);if (res.type == MKAddrInfo.MK_GEOCODE) {// 地理编码:通过地址检索坐标点String strInfo = String.format("纬度:%f 经度:%f",res.geoPt.getLatitudeE6() / 1e6,res.geoPt.getLongitudeE6() / 1e6);Toast.makeText(GeoCoderActivity.this, strInfo,Toast.LENGTH_LONG).show();}if (res.type == MKAddrInfo.MK_REVERSEGEOCODE) {// 反地理编码:通过坐标点检索详细地址及周边poiString strInfo = res.strAddr;Toast.makeText(GeoCoderActivity.this, strInfo,Toast.LENGTH_LONG).show();}// 生成ItemizedOverlay图层用来标注结果点ItemizedOverlay<OverlayItem> itemOverlay = new ItemizedOverlay<OverlayItem>(null, mMapView);// 生成ItemOverlayItem item = new OverlayItem(res.geoPt, "", null);// 得到需要标在地图上的资源Drawable marker = getResources().getDrawable(R.drawable.icon_markf);// 为maker定义位置和边界marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());// 给item设置markeritem.setMarker(marker);// 在图层上添加itemitemOverlay.addItem(item);// 清除地图其他图层mMapView.getOverlays().clear();// 添加一个标注ItemizedOverlay图层mMapView.getOverlays().add(itemOverlay);// 执行刷新使生效mMapView.refresh();}public void onGetPoiResult(MKPoiResult res, int type, int error) {}public void onGetDrivingRouteResult(MKDrivingRouteResult res,int error) {}public void onGetTransitRouteResult(MKTransitRouteResult res,int error) {}public void onGetWalkingRouteResult(MKWalkingRouteResult res,int error) {}public void onGetBusDetailResult(MKBusLineResult result, int iError) {}@Overridepublic void onGetSuggestionResult(MKSuggestionResult res, int arg1) {}@Overridepublic void onGetShareUrlResult(MKShareUrlResult result, int type,int error) {}});}@Overridepublic void onClick(View v) {if (v == mBtnGeoCode) {// Geo搜索mSearch.geocode(editGeoCodeKey.getText().toString(), editCity.getText().toString());}if (v == mBtnReverseGeoCode) {GeoPoint ptCenter = new GeoPoint((int) (Float.valueOf(lat.getText().toString()) * 1e6), (int) (Float.valueOf(lon.getText().toString()) * 1e6));// 反Geo搜索mSearch.reverseGeocode(ptCenter);}}@Overrideprotected void onPause() {mMapView.onPause();super.onPause();}@Overrideprotected void onResume() {mMapView.onResume();super.onResume();}@Overrideprotected void onDestroy() {mMapView.destroy();super.onDestroy();}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mMapView.onSaveInstanceState(outState);}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);mMapView.onRestoreInstanceState(savedInstanceState);}}
布局XML:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/geocoder_et_city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" /> <EditText android:id="@+id/geocoder_et_geocodekey" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="海淀区上地十街10号" /> <Button android:id="@+id/geocoder_btn_geocode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_style" android:text="Geo" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/geocoder_et_lat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="39.904965" /> <EditText android:id="@+id/geocoder_et_lon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="116.327764" /> <Button android:id="@+id/geocoder_btn_reversegeocode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_style" android:text="ReverseGeo" /> </LinearLayout> <com.baidu.mapapi.map.MapView android:id="@+id/geocoder_bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" /></LinearLayout>
配置文件同之前地图示例
附上图片效果:
- 百度地图之地址信息和坐标的转换
- BaiduMap---百度地图官方Demo之地理编码功能(介绍地址信息和坐标之间的相互转换)
- 关于android开发中百度地图地址转换坐标信息的教程
- 百度地图根据坐标获取地址信息
- 百度地图和高德地图的坐标转换
- 百度地图坐标和高德地图坐标转换
- 百度地图静态图之坐标转换
- iOS 百度地图之坐标转换
- 百度地图开发之 坐标转换
- 百度地图API地理位置和坐标转换
- 百度地图坐标转换
- 百度地图坐标转换
- 百度地图坐标转换
- 百度地图坐标转换
- 百度地图坐标转换
- 百度地图导航与百度地图进行的坐标转换
- IOS百度地图的坐标转换
- 百度地图之批量转换成百度坐标
- oracle在查询结果中附加一列的实现方法
- excel学习札记三---冻结窗格
- cubieboard(A20)显示模块初始化分析
- Magento获取Controller和ActionName的方法
- js 实现繁体和简体之间的转换
- 百度地图之地址信息和坐标的转换
- 自己写的HTML5 canvas动画制作函数,非常简单易懂
- The format of string resources
- Linux cat 命令
- 嵌入式系统中通过pppd拨号后的dns设置
- 新插件CopyPC2U已经完成!
- django admin manytomany获取所选字段值
- iOS开发者必备:九大设计类工具
- Highcharts 参数设置