一:百度地图开发必须要到百度开发平台android开发api下载相应的库,已经申请百度地图开发key.
二:新建项目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" >
-
- <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
- 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
- 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;
- public 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接口,代码如下:
package com.location.activity;
- 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;
-
- @Override
- public 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) {
- @Override
- public 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() {
-
-
- mapManager = new BMapManager(getApplication());
-
- mapManager.init("C66C0501D0280744759A6957C42543AE38F5D540", null);
- super.initMapActivity(mapManager);
-
- mMKSearch = new MKSearch();
-
- mMKSearch.init(mapManager, new MySearchListener());
- mLocationManager = mapManager.getLocationManager();
-
- mLocationManager.requestLocationUpdates(this);
-
- mLocationManager
- .enableProvider((int) MKLocationManager.MK_GPS_PROVIDER);
- }
-
- @Override
- protected void onResume() {
- if (mapManager != null) {
- mapManager.start();
- }
- super.onResume();
-
- }
-
- @Override
- protected void onPause() {
- isLoadAdrr = false;
- if (mapManager != null) {
- mapManager.stop();
- }
- super.onPause();
- }
-
- @Override
- protected boolean isRouteDisplayed() {
-
- return false;
- }
-
-
-
- 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() {
- @Override
- public 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;
- }
- }
- };
-
-
- @Override
- protected void onDestroy() {
- if (mapManager != null) {
- mapManager.destroy();
- mapManager = null;
- }
- super.onDestroy();
- }
-
-
- @Override
- protected boolean isLocationDisplayed() {
- return false;
- }
-
-
- public void onLocationChanged(Location location) {
- if (location != null) {
- locPoint = new GeoPoint((int) (location.getLatitude()* 1E6),
- (int) (location.getLongitude()* 1E6));
- mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
- }
- }
-
-
- public class MySearchListener implements MKSearchListener {
-
- 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);
-
- }
-
-
- public void onGetDrivingRouteResult(MKDrivingRouteResult result,
- int iError) {
- }
-
-
- public void onGetPoiResult(MKPoiResult result, int type, int iError) {
- }
-
-
- public void onGetTransitRouteResult(MKTransitRouteResult result,
- int iError) {
- }
-
-
- public void onGetWalkingRouteResult(MKWalkingRouteResult result,
- int iError) {
- }
-
- public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
-
-
- }
-
- public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
-
-
- }
- }
-
- }
五:在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" />
-
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
-
- <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" />
六:运行结果如下图: