(android地图开发) 基于百度Lbs服务定位用户的当前位置
不点
阅读:573
2021-04-01 10:21:01
评论:0
第一步:搭建百度地图(LBS)服务
第二步:配置百度Lbs服务权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rf.mapabc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="7" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--配置密钥 -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="3f4f2c4d98836da847b7c3f7b8302450" />
<activity
android:name="com.rf.mapabc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Baidu地图Lbs服务 -->
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">
</service>
</application>
<!-- mapabc地图权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<!--百度地图Lbs服务权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
<uses-permission android:name="android.permission.ACCES_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
</manifest>
效果截图:
源代码:
package com.rf.mapabc;
import com.amap.mapapi.core.GeoPoint;
import com.amap.mapapi.map.MapActivity;
import com.amap.mapapi.map.MapController;
import com.amap.mapapi.map.MapView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
public class MainActivity extends MapActivity {
private MapView mMapView;
private MapController mMapController;
private GeoPoint point;
// 百度地图LBS定位
private LocationClient mLocationClient;
private MyLocationListener locationlistener;
// 主线程Handler
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 0:
Bundle bundle = msg.getData();
double lat = bundle.getDouble("x");
double lng = bundle.getDouble("y");
point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
Toast.makeText(MainActivity.this,
"相关经纬度:" + lat + "\r\t" + lng, Toast.LENGTH_LONG)
.show();
mMapController.setCenter(point); // 设置地图中心点
mMapController.setZoom(12); // 设置地图zoom级别
break;
}
}
};
@Override
/**
*显示栅格地图,启用内置缩放控件,并用MapController控制地图的中心点及Zoom级别
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
// 获取地图组件
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
point = new GeoPoint((int) (39.982378 * 1E6), (int) (116.304923 * 1E6)); // 用给定的经纬度构造一个GeoPoint,单位是微度
// (度
// *
// 1E6)
mMapController.setCenter(point); // 设置地图中心点
mMapController.setZoom(12); // 设置地图zoom级别
// 百度地位
mLocationClient = new LocationClient(this);
locationlistener = new MyLocationListener(handler);
mLocationClient.registerLocationListener(locationlistener);
mLocationClient.start();
if (mLocationClient != null && mLocationClient.isStarted()) {
setLocationOption();
mLocationClient.requestLocation();
}
}
// 实时位置的显示的问题(LBS)
public class MyLocationListener implements BDLocationListener {
private Handler handle;
public MyLocationListener(Handler handle) {
super();
this.handle = handle;
}
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
//获取经纬度
double x = location.getLatitude();
double y = location.getLongitude();
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putDouble("x", x);
bundle.putDouble("y", y);
message.setData(bundle);
message.what = 0;
handle.sendMessage(message);
}
@Override
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
}
}
// 查询条件显示
private void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("all");// 返回的定位结果包含地址信息
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
option.disableCache(true);// 禁止启用缓存定位
option.setPoiNumber(5); // 最多返回POI个数
option.setPoiDistance(1000); // poi查询距离
option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
mLocationClient.setLocOption(option);
}
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。