百度地图API android开发之地图定位

百度地图API android开发之地图定位

  • java代码
package com.example.baidumaptest;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private MapView mMapView=null;
    LocationClient  mLocClient;
    BaiduMap mBaiduMap;
    boolean isFirstLoc = true;// 是否首次定位
    Button GPS_btn;

    public MyLocationListenner myListener = new MyLocationListenner();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();      
        mBaiduMap.setMaxAndMinZoomLevel(19, 11);//设置缩放级别
        GPS_btn=(Button)findViewById(R.id.GPS_btn);
        GPS_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
           // 开启定位图层
            mBaiduMap.setMyLocationEnabled(true);
          // 定位初始化
           mLocClient = new LocationClient(MainActivity.this);
           mLocClient.registerLocationListener(myListener);
           LocationClientOption option = new LocationClientOption();
           option.setOpenGps(true);// 打开GPS
           option.setCoorType("bd09ll"); // 设置坐标类型
           option.setScanSpan(1000);
           mLocClient.setLocOption(option);
           mLocClient.start();
            }
        });     
    }

    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null)
                return;
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(13).build()));
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                mBaiduMap.animateMapStatus(u);
              }
            }
            public void onReceivePoi(BDLocation poiLocation) {
            }
        }
        @Override
        protected void onPause() {
            mMapView.onPause();
            super.onPause();
        }

        @Override
        protected void onResume() {
            mMapView.onResume();
            super.onResume();
        }

        @Override
        protected void onDestroy() {
            // 退出时销毁定位
            mLocClient.stop();
            // 关闭定位图层
            mBaiduMap.setMyLocationEnabled(false);
            mMapView.onDestroy();
            mMapView = null;
            super.onDestroy();
        }

}
  • main.xml就是添加一个button布局
"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.baidu.mapapi.map.MapView  
    android:id="@+id/bmapView"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:clickable="true" />
        
  • button的样式

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:drawable="@drawable/ic_location" android:state_pressed="false"/>
      <item android:drawable="@drawable/ic_location_press" android:state_pressed="true"/>

selector>
  • 最后是AndroidManifest.xml添加所需要的权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
  • 在application里添加meta-data
 "com.baidu.lbsapi.API_KEY"  
        android:value="oBCspIGwnStpqULGPiy5qMzV" />//此处是自己申请的百度开发key

在百度地图开发中遇到MapView布局是加载失败,只要在libs目录下添加一个文件夹armeabi-v7a,把so文件复制进去就行,如图百度地图API android开发之地图定位_第1张图片
百度地图API android开发之地图定位_第2张图片
在模拟器不能定位,只能用真机来测试,附上demo
baidumaptest

你可能感兴趣的:(android,for,百度地图API开发)