当前位置: 代码迷 >> Android >> android 基于百度mapapi开发定位以及获取详细地址
  详细解决方案

android 基于百度mapapi开发定位以及获取详细地址

热度:57   发布时间:2016-05-01 13:31:45.0
android 基于百度地图api开发定位以及获取详细地址

一:百度地图开发必须要到百度开发平台android开发api下载相应的库,已经申请百度地图开发key,在这个博客里面有详细的说明和演示,(如果不懂得请看此文章)?http://104zz.iteye.com/blog/1680781

二:新建项目baidumaplocation.设计main.xml文件这里注意的是MapView控件必须使用来自百度库封装好的com.baidu.mapapi.MapView?。设计代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <FrameLayout        android:id="@+id/map_layout"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <!-- 百度MapView控件 -->        <com.baidu.mapapi.MapView            android:id="@+id/map_view"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:apiKey="0Mg_koWoyZUiYLfZxmPfp4LKInB5LqTnagYueaw"            android:clickable="true"            android:enabled="true" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:orientation="vertical"            android:paddingBottom="105dip" >            <!-- 地址信息显示TextView -->            <TextView                android:id="@+id/map_bubbleText"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/location_tips"                android:gravity="left|center"                android:maxEms="12"                android:paddingLeft="12dip"                android:paddingRight="10dip"                android:text="@string/load_tips"                android:textColor="#cfcfcf"                android:textSize="14sp" />        </LinearLayout>        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:orientation="vertical" >            <!-- 位置指标显示ImageView -->            <ImageView                android:id="@+id/point_image"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:layout_marginBottom="30dip"                android:src="@drawable/point_start" />        </LinearLayout>    </FrameLayout></LinearLayout>
?

三:创建覆盖整个地图捕捉触控事件的MyMapOverlay继承Overlay

import android.view.MotionEvent;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.MapView;import com.baidu.mapapi.Overlay;//覆盖整个地图捕捉触控事件的OverLaypublic abstract class MyMapOverlay extends Overlay{private int point_X;private int point_Y;private GeoPoint newPoint;public MyMapOverlay(int x,int y){point_X = x;point_Y = y;}boolean flagMove=false;//这里实现根据地图移动时重新获取屏幕中心点的经纬度坐标    @Override     public boolean onTouchEvent(MotionEvent event, MapView mapView) {    	System.out.println("X->"+event.getX()+":"+point_X);    	System.out.println("Y->"+event.getY()+":"+point_Y);        if(event.getAction() == MotionEvent.ACTION_DOWN){        	changePoint(newPoint,1);        }else if(event.getAction() == MotionEvent.ACTION_UP){        	newPoint = mapView.getProjection().fromPixels(point_X,point_Y);        	changePoint(newPoint,2);        }               return false;    }        public abstract void changePoint(GeoPoint newPoint,int type);}
?

四:LocationActivity类继承百度库的MapActivity以及实现LocationListener接口,代码如下:

import java.io.IOException;import java.util.List;import java.util.Locale;import android.content.Intent;import android.location.Address;import android.location.Geocoder;import android.location.Location;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.Window;import android.widget.TextView;import com.android.map.MyMapOverlay;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.LocationListener;import com.baidu.mapapi.MKAddrInfo;import com.baidu.mapapi.MKBusLineResult;import com.baidu.mapapi.MKDrivingRouteResult;import com.baidu.mapapi.MKLocationManager;import com.baidu.mapapi.MKPoiResult;import com.baidu.mapapi.MKSearch;import com.baidu.mapapi.MKSearchListener;import com.baidu.mapapi.MKSuggestionResult;import com.baidu.mapapi.MKTransitRouteResult;import com.baidu.mapapi.MKWalkingRouteResult;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.Overlay;public class LocationActivity extends MapActivity implements LocationListener {private MapView mapView;private MapController mMapCtrl;private List<Overlay> mapOverlays;public GeoPoint locPoint;private MyMapOverlay mOverlay;private TextView desText;private String lost_tips;private int point_X;private int point_Y;public final int MSG_VIEW_LONGPRESS = 10001;public final int MSG_VIEW_ADDRESSNAME = 10002;public final int MSG_GONE_ADDRESSNAME = 10003;private Intent mIntent;private int mLatitude;private int mLongitude;private String name;private BMapManager mapManager;private MKLocationManager mLocationManager = null;private boolean isLoadAdrr = true;private MKSearch mMKSearch;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);initMap();mIntent = getIntent();mLatitude = mIntent.getIntExtra("latitude", 0);mLongitude = mIntent.getIntExtra("longitude", 0);name = mIntent.getStringExtra("name");mapView = (MapView) findViewById(R.id.map_view);desText = (TextView) this.findViewById(R.id.map_bubbleText);lost_tips = getResources().getString(R.string.load_tips);if (mLatitude != 0 && mLongitude != 0) {locPoint = new GeoPoint((int) (mLatitude * 1E6),(int) (mLongitude * 1E6));desText.setText(name);}mapView.setBuiltInZoomControls(true);mapView.setClickable(true);mMapCtrl = mapView.getController();point_X = this.getWindowManager().getDefaultDisplay().getWidth() / 2;point_Y = this.getWindowManager().getDefaultDisplay().getHeight() / 2;mOverlay = new MyMapOverlay(point_X, point_Y) {@Overridepublic void changePoint(GeoPoint newPoint, int type) {if (type == 1) {mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME);} else {locPoint = newPoint;mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);}}};mapOverlays = mapView.getOverlays();if (mapOverlays.size() > 0) {mapOverlays.clear();}mapOverlays.add(mOverlay);mMapCtrl.setZoom(20);}private void initMap() {// 初始化MapActivitymapManager = new BMapManager(getApplication());// init方法的第一个参数需填入申请的API KeymapManager.init("C66C0501D0280744759A6957C42543AE38F5D540", null);super.initMapActivity(mapManager);// 实例化搜索地址类mMKSearch = new MKSearch();// 初始化搜索地址实例mMKSearch.init(mapManager, new MySearchListener());mLocationManager = mapManager.getLocationManager();// 注册位置更新事件mLocationManager.requestLocationUpdates(this);// 使用GPS定位mLocationManager.enableProvider((int) MKLocationManager.MK_GPS_PROVIDER);}@Overrideprotected void onResume() {if (mapManager != null) {mapManager.start();}super.onResume();}@Overrideprotected void onPause() {isLoadAdrr = false;if (mapManager != null) {mapManager.stop();}super.onPause();}@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn false;}/** * 通过经纬度获取地址 *  * @param point * @return */private String getLocationAddress(GeoPoint point) {String add = "";Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());try {List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,1);Address address = addresses.get(0);int maxLine = address.getMaxAddressLineIndex();if (maxLine >= 2) {add = address.getAddressLine(1) + address.getAddressLine(2);} else {add = address.getAddressLine(1);}} catch (IOException e) {add = "";e.printStackTrace();}return add;}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_VIEW_LONGPRESS:// 处理长按时间返回位置信息{if (null == locPoint)return;mMKSearch.reverseGeocode(locPoint);desText.setVisibility(View.VISIBLE);desText.setText(lost_tips);mMapCtrl.animateTo(locPoint);mapView.invalidate();}break;case MSG_VIEW_ADDRESSNAME:desText.setText((String) msg.obj);desText.setVisibility(View.VISIBLE);break;case MSG_GONE_ADDRESSNAME:desText.setVisibility(View.GONE);break;}}};// 关闭程序也关闭定位@Overrideprotected void onDestroy() {if (mapManager != null) {mapManager.destroy();mapManager = null;}super.onDestroy();}/** * 根据MyLocationOverlay配置的属性确定是否在地图上显示当前位置 */@Overrideprotected boolean isLocationDisplayed() {return false;}/** * 当位置发生变化时触发此方法 *  * @param location *            当前位置 */public void onLocationChanged(Location location) {if (location != null) {locPoint = new GeoPoint((int) (location.getLatitude()* 1E6),(int) (location.getLongitude()* 1E6));mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);}}/** * 内部类实现MKSearchListener接口,用于实现异步搜索服务 *  * @author liufeng */public class MySearchListener implements MKSearchListener {/** * 根据经纬度搜索地址信息结果 *  * @param result *            搜索结果 * @param iError *            错误号(0表示正确返回) */public void onGetAddrResult(MKAddrInfo result, int iError) {if (result == null) {return;}Message msg = new Message();msg.what = MSG_VIEW_ADDRESSNAME;msg.obj = result.strAddr;mHandler.sendMessage(msg);}/** * 驾车路线搜索结果 *  * @param result *            搜索结果 * @param iError *            错误号(0表示正确返回) */public void onGetDrivingRouteResult(MKDrivingRouteResult result,int iError) {}/** * POI搜索结果(范围检索、城市POI检索、周边检索) *  * @param result *            搜索结果 * @param type *            返回结果类型(11,12,21:poi列表 7:城市列表) * @param iError *            错误号(0表示正确返回) */public void onGetPoiResult(MKPoiResult result, int type, int iError) {}/** * 公交换乘路线搜索结果 *  * @param result *            搜索结果 * @param iError *            错误号(0表示正确返回) */public void onGetTransitRouteResult(MKTransitRouteResult result,int iError) {}/** * 步行路线搜索结果 *  * @param result *            搜索结果 * @param iError *            错误号(0表示正确返回) */public void onGetWalkingRouteResult(MKWalkingRouteResult result,int iError) {}public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {// TODO Auto-generated method stub}public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {// TODO Auto-generated method stub}}}
?

五:在AndroidManifest.xml住添加相关的访问权限

?<!-- 访问网络的权限 -->

    <uses-permission android:name="android.permission.INTERNET" />    <!-- 访问精确位置的权限 -->    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <!-- 访问网络状态的权限 -->    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <!-- 访问WIFI网络状态的权限 -->    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <!-- 改变WIFI网络状态的权限 -->    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <!-- 读写存储卡的权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <!-- 读取电话状态的权限 --><uses-permission android:name="android.permission.READ_PHONE_STATE" />
?

六:运行结果如下图:


  相关解决方案