百度地图实现简单的定位功能

来源:互联网 发布:小漠淘宝店 编辑:程序博客网 时间:2024/04/29 16:53

需要使用到百度地图开发者平台

然后在xml中写一个button点击进行定位

 <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="点击定位" />    <com.baidu.mapapi.map.MapView        android:id="@+id/mapview"        android:layout_width="match_parent"        android:layout_height="match_parent">

添加button的点击事件
  public void onClick(View view) {         mLocationClient = new LocationClient(this);        mBdLocationListener = new BDLocationListener() {            @Override            public void onReceiveLocation(BDLocation bdLocation) {                MarkerOptions markerOptions = new MarkerOptions();                markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));                latitude = bdLocation.getLatitude();                longitude = bdLocation.getLongitude();//通过百度地图定位服务获取当前位置的经纬度                markerOptions.position(new LatLng(latitude, longitude));                map.addOverlay(markerOptions);            } };        MapStatus mapStatus = new MapStatus.Builder()                .target(new LatLng(latitude,longitude))                .zoom(14).build();//zoom设置显示放大的级别,target设置显示位置的经纬度        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);        map.setMapStatus(mapStatusUpdate);        mLocationClient.registerLocationListener(mBdLocationListener);//此方法用于更新地图显示的位置        mLocationClient.start();        }

具体定位和地图怎么实现可以看http://blog.csdn.net/qq_29575707/article/details/52786896

源码http://download.csdn.net/detail/qq_29575707/9651176

http://download.csdn.net/detail/qq_29575707/9651169

0 0
原创粉丝点击