作者:此恨缠绵_793 | 来源:互联网 | 2023-10-13 17:24
步骤:
1.注册百度地图开发者平台,申请API_KEY,下载定位SDK,
2.把SDK中libs中的文件全部复制到项目中的libs文件夹下。。。。。(此步骤请参考我的上一个博客)。
3.在Androidmanifest 文件中加入一下代码, 要注意接入的位置,是在第一个Application中哦
<service
android:name&#61;"com.baidu.location.f"android:enabled&#61;"true"android:process&#61;":remote">service>
然后我贴一个完整的代码和布局文件吧&#xff0c;
MainActivity:
public class MainActivity extends Activity implements OnClickListener{private TextView mText;private LocationClient mLocationClient &#61; null;private BDLocationListener myListener &#61; new MyLocationListener();private MapView mMapView;public BaiduMap mBaiduMap;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);SDKInitializer.initialize(getApplicationContext());setContentView(R.layout.activity_main);mLocationClient &#61; new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(myListener); initWidgets();setLocationOption();mLocationClient.start();mMapView &#61; (MapView) findViewById(R.id.bmapView);mBaiduMap &#61; mMapView.getMap();}private void initWidgets() {mText &#61; (TextView) findViewById(R.id.tv_text);Button btn &#61; (Button) findViewById(R.id.btn_request);btn.setOnClickListener(this);}&#64;Overrideprotected void onDestroy() {super.onDestroy();mLocationClient.stop();}private void setLocationOption() {LocationClientOption option &#61; new LocationClientOption();option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);option.setCoorType("bd09ll");int span&#61;1000;option.setScanSpan(span);option.setIsNeedAddress(true);option.setOpenGps(true);option.setLocationNotify(true);option.setIsNeedLocationDescribe(true);option.setIsNeedLocationPoiList(true);option.setIgnoreKillProcess(false);option.SetIgnoreCacheException(false);option.setEnableSimulateGps(false);mLocationClient.setLocOption(option);}public class MyLocationListener implements BDLocationListener {&#64;Overridepublic void onReceiveLocation(BDLocation location) {if (location &#61;&#61; null)return;StringBuffer sb &#61; new StringBuffer(256);sb.append("当前时间 : ");sb.append(location.getTime());sb.append("\n错误码 : ");sb.append(location.getLocType());sb.append("\n纬度 : ");sb.append(location.getLatitude());sb.append("\n经度 : ");sb.append(location.getLongitude());sb.append("\n半径 : ");sb.append(location.getRadius());if (location.getLocType() &#61;&#61; BDLocation.TypeGpsLocation) {sb.append("\n速度 : ");sb.append(location.getSpeed());sb.append("\n卫星数 : ");sb.append(location.getSatelliteNumber());} else if (location.getLocType() &#61;&#61; BDLocation.TypeNetWorkLocation) {sb.append("\n地址 : ");sb.append(location.getAddrStr());}mText.setText(sb.toString());LatLng cenpt &#61; new LatLng(location.getLatitude(),location.getLongitude());MapStatus mMapStatus &#61; new MapStatus.Builder().target(cenpt).zoom(18).build();MapStatusUpdate mMapStatusUpdate &#61; MapStatusUpdateFactory.newMapStatus(mMapStatus);mBaiduMap.setMapStatus(mMapStatusUpdate);LatLng point &#61; new LatLng(location.getLatitude(), location.getLongitude());BitmapDescriptor bitmap &#61; BitmapDescriptorFactory.fromResource(R.drawable.mark1);OverlayOptions option &#61; new MarkerOptions().position(point).icon(bitmap);mBaiduMap.addOverlay(option);}&#64;Overridepublic void onConnectHotSpotMessage(String s, int i) {}}&#64;Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_request:if (mLocationClient !&#61; null ){mLocationClient.registerLocationListener(myListener);if(mLocationClient.isStarted()&#61;&#61;false){mLocationClient.start();}mLocationClient.requestLocation();}break;}}
}
布局文件&#xff1a;
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"tools:context&#61;"zsw.com.locationtest1.MainActivity" ><com.baidu.mapapi.map.MapView
android:layout_weight&#61;"1"android:id&#61;"&#64;&#43;id/bmapView"android:layout_width&#61;"fill_parent"android:layout_height&#61;"fill_parent"android:clickable&#61;"true" /><Button
android:text&#61;"定位"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:id&#61;"&#64;&#43;id/btn_request" /><TextView android:layout_weight&#61;"3"android:text&#61;"TextView"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:id&#61;"&#64;&#43;id/tv_text" />
LinearLayout>
manifest文件
<manifest xmlns:android&#61;"http://schemas.android.com/apk/res/android"package&#61;"zsw.com.locationtest1"><application
android:allowBackup&#61;"true"android:icon&#61;"&#64;mipmap/ic_launcher"android:label&#61;"&#64;string/app_name"android:supportsRtl&#61;"true"android:theme&#61;"&#64;style/AppTheme"><service
android:name&#61;"com.baidu.location.f"android:enabled&#61;"true"android:process&#61;":remote">service><meta-data
android:name&#61;"com.baidu.lbsapi.API_KEY"android:value&#61;"G8CCDPLQTEEuOEMpO8Ru90HDowmNUXUP" />//key:开发者申请的Key<activity android:name&#61;".MainActivity"><intent-filter><action android:name&#61;"android.intent.action.MAIN" /><category android:name&#61;"android.intent.category.LAUNCHER" />intent-filter>activity>application><uses-permission android:name&#61;"android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name&#61;"android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name&#61;"android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name&#61;"android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name&#61;"android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name&#61;"android.permission.READ_PHONE_STATE" /><uses-permission android:name&#61;"android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name&#61;"android.permission.INTERNET" /><uses-permission android:name&#61;"android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
manifest>
总结&#xff1a;如果发现自己定位到了非洲&#xff0c;如果Android版本是6.0以上&#xff0c;而因为定位权限是敏感权限&#xff0c;需要动态授权&#xff0c;如果过你用的是模拟器的话&#xff0c;也有可能会定位到非洲的&#xff0c;所以请用真机测试。