此文章接上一篇文件:安卓百度地图之定位_zpon_wangxuan的博客-CSDN博客,对于地图上显示定位图标就是在地图上添加一个地图的marker就可以了;
布局文件代码:
<LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/bg_color_login"android:orientation="vertical"tools:context=".UnLoginActiviy" ><includeandroid:id="@+id/rl_title"layout="@layout/title_layout" /><com.baidu.mapapi.map.MapViewandroid:id="@+id/bmapView"android:layout_width="match_parent"android:layout_height="match_parent"android:clickable="true" /></LinearLayout>
java文件代码:
package com.meal;import android.Manifest;import android.app.Activity;import android.content.pm.PackageManager;import android.os.Bundle;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.util.Log;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import com.baidu.location.BDAbstractLocationListener;import com.baidu.location.BDLocation;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.MapStatus;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.Marker;import com.baidu.mapapi.map.MarkerOptions;import com.baidu.mapapi.map.MyLocationData;import com.baidu.mapapi.model.LatLng;import com.pony.base.BaseActivity;import com.pony.util.ToastUtil;import java.util.ArrayList;import java.util.List;public class ShowMapActivity extends BaseActivity {// titleprivate TextView mTvTitle;// 返回private ImageView mIvBack;String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE,};private static final int ACCESS_FINE_LOCATION = 1;private static final int WRITE_EXTERNAL_STORAGE = 2;List<String> mPermissionList = new ArrayList<String>();public LocationClient mLocationClient = null;private MyLocationListener myListener = new MyLocationListener();private MapView mMapView = null;private BaiduMap baiduMap;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_showmap);initWidget();initData();checkPermission();}@Overridepublic void initWidget() {mIvBack = (ImageView) findViewById(R.id.mIvBack);mTvTitle = (TextView) findViewById(R.id.mTvTitle);mTvTitle.setText("地图显示");mIvBack.setVisibility(View.VISIBLE);mIvBack.setOnClickListener(this);mMapView = (MapView) findViewById(R.id.bmapView);baiduMap = mMapView.getMap();baiduMap.setMyLocationEnabled(true);mLocationClient = new LocationClient(getApplicationContext());//声明LocationClient类mLocationClient.registerLocationListener(myListener);LocationClientOption option = new LocationClientOption();option.setIsNeedAddress(true);option.setScanSpan(1000);option.setNeedNewVersionRgc(true);mLocationClient.setLocOption(option);mLocationClient.start();}@Overridepublic void initData() {}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.mIvBack:this.finish();break;}}// 是否首次定位private boolean isFirstLoc = true;public class MyLocationListener extends BDAbstractLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {//此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果//以下只列举部分获取地址相关的结果信息//更多结果信息获取说明,请参照类参考中BDLocation类中的说明String addr = location.getAddrStr(); //获取详细地址信息String country = location.getCountry(); //获取国家String province = location.getProvince(); //获取省份String city = location.getCity(); //获取城市String district = location.getDistrict(); //获取区县String street = location.getStreet(); //获取街道信息String adcode = location.getAdCode(); //获取adcodeString town = location.getTown(); //获取乡镇信息Log.i("poy_log", addr);MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())// 此处设置开发者获取到的方向信息,顺时针0-360.direction(location.getDirection()).latitude(location.getLatitude()).longitude(location.getLongitude()).build();baiduMap.setMyLocationData(locData);if (isFirstLoc) {isFirstLoc = false;//LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());MapStatus.Builder builder = new MapStatus.Builder();builder.target(ll).zoom(15.0f);baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));}}}@Overrideprotected void onResume() {super.onResume();//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理mMapView.onResume();}@Overrideprotected void onPause() {super.onPause();//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理mMapView.onPause();}@Overrideprotected void onDestroy() {super.onDestroy();//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理mLocationClient.stop();baiduMap.setMyLocationEnabled(false);mMapView.onDestroy();mMapView = null;}private void checkPermission() {for (int i = 0; i < permissions.length; i++) {if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {mPermissionList.add(permissions[i]);}}if (mPermissionList.isEmpty()) {//未授予的权限为空,表示都授予了initWidget();initData();} else {//请求权限方法String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组ActivityCompat.requestPermissions(this, permissions, ACCESS_FINE_LOCATION);}}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {if (requestCode == ACCESS_FINE_LOCATION) {if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {initWidget();initData();} else {ToastUtil.show(this, "权限已拒绝");}} else if (requestCode == WRITE_EXTERNAL_STORAGE) {for (int i = 0; i < grantResults.length; i++) {if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {//判断是否勾选禁止后不再询问boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i]);if (showRequestPermission) {ToastUtil.show(this, "权限未申请");}}}}}}