百度地图

来源:互联网 发布:天空之城软件站 编辑:程序博客网 时间:2024/06/07 21:10

百度地图

1.建立工程

建立Android工程 BaiduMap 改包名为 com.baiduMap

2.申请 API key

首先注册成为百度开发者 https://developer.baidu.com/ 后进入 http://lbsyun.baidu.com/ 点击 开发 ->Android地图SDK ->获取秘钥 创建应用
  • 应用名称 地图(随便填)
  • 应用类型 Android SDK
  • 发布版SHA1
    • Eclipse开发 windows -> preferance -> android -> build 中的 SH1 fingerprint中的内容
    • Android Studio 开发 project ->android ->Gradle Scripts ->Gradle ->Tasks -> android ->signingReport 在下边可看到 SH1
  • 包名 com.baiduMap
  • 详情见开发指南

3.显示地图

  • 添加 API Key
<meta-data                      android:name="com.baidu.lbsapi.API_KEY"        android:value="G71Q1ISdKseIvrsSLvjH2wYRSdC2Rwkd" />
  • 添加布局
<com.baidu.mapapi.map.MapView        android:id="@+id/bmapView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:clickable="true" />
  • 在MainActivity 中添加
public class MainActivity extends AppCompatActivity {    MapView mMapView = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        SDKInitializer.initialize(getApplicationContext());        setContentView(R.layout.activity_main);        mMapView = (MapView) findViewById(R.id.bmapView);    @Override    protected void onDestroy() {        super.onDestroy();        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理        mMapView.onDestroy();    }    @Override    protected void onResume() {        super.onResume();        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理        mMapView.onResume();    }    @Override    protected void onPause() {        super.onPause();        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理        mMapView.onPause();    }}
  • 添加权限 防止程序挂掉全部都添加
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <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" />

4.定位到我的位置

  • 在MianActivity中添加
public class MainActivity extends AppCompatActivity {    private LocationManager locationManager;    private String provider;    BaiduMap baiduMap;    ...    @Override    protected void onCreate(Bundle savedInstanceState) {        ...        // 获取baiduMap对象        baiduMap = mMapView.getMap();        // 设置可改变地图位置        baiduMap.setMyLocationEnabled(true);        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        List<String> list = locationManager.getProviders(true);        //判断是否有GPS 网络 获取的地图        if (list.contains(LocationManager.NETWORK_PROVIDER))            provider = LocationManager.NETWORK_PROVIDER;        else if (list.contains(LocationManager.GPS_PROVIDER))            provider = LocationManager.GPS_PROVIDER;        else            Toast.makeText(getApplicationContext(), "No location provider to use", Toast.LENGTH_SHORT).show();        //该if语句是错误提示后系统自动添加        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            // TODO: Consider calling            //    ActivityCompat#requestPermissions            // here to request the missing permissions, and then overriding            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,            //                                          int[] grantResults)            // to handle the case where the user grants the permission. See the documentation            // for ActivityCompat#requestPermissions for more details.            return;        }        Location location = locationManager.getLastKnownLocation(provider);        if(location != null) {            navigateTo(location);        }    }    private void navigateTo(Location location){        double y = location.getLatitude() ;        double x = location.getLongitude() ;        LatLng ll = new LatLng(y,x);        MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);        // 移动到某经纬度        baiduMap.animateMapStatus(update);        update = MapStatusUpdateFactory.zoomBy(5f);        // 放大        baiduMap.animateMapStatus(update);    }    ...}

5.覆盖物覆盖我的位置

  • 在MainActivity 中 private void navigateTo(Location location)中添加
private void navigateTo(Location location){        ...        // 构造定位数据        MyLocationData locData = new MyLocationData.Builder()                .accuracy(location.getAccuracy())                // 此处设置开发者获取到的方向信息,顺时针0-360                .direction(0).latitude(x)                .longitude(y).build();        // 设置定位数据        baiduMap.setMyLocationData(locData);        // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ioc);        MyLocationConfiguration config = new MyLocationConfiguration( MyLocationConfiguration.LocationMode.NORMAL, true, bitmap);        baiduMap.setMyLocationConfiguration(config);        // 当不需要定位图层时关闭定位图层       // baiduMap.setMyLocationEnabled(false);}

6 优化 location为空时无法定位

  • Location location = locationManager.getLastKnownLocation(provider);后添加
while (location == null) {            locationManager.requestLocationUpdates("gps", 60000, 1, locationListener);        }
  • 在MainActivity 中添加
private final LocationListener locationListener = new LocationListener() {        //位置发生改变后调用        public void onLocationChanged(Location location) {            //更新当前设备的新位置信息            navigateTo(location);        }        //provider 被用户关闭后调用        public void onProviderDisabled(String provider) {        }        //provider 被用户开启后调用        public void onProviderEnabled(String provider) {        }        //provider 状态变化时调用        public void onStatusChanged(String provider, int status, Bundle extras) {        }    };
原创粉丝点击