Android百度地图调用和GPS定位
16lz
2021-01-23
1、声明权限内容
其中的百度地图的key需要在百度地图的开发者上申请
申请key需要用到包名和SHA1
Window→preference→Android→Build 中有
package="包名" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.GET_TASKS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="5Qnu8ADVqkw4KXdbdMK9vgxSqsuaFTKm" /> <activity android:name=".LoginActivity" android:label="@string/app_name" android:launchMode="singleTask" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> <activity android:name=".MainActivity" android:icon="@null" android:label="@string/back" android:launchMode="singleTask" > activity> <activity android:name="driver.LoginDriver" android:label="@string/title_activity_login_driver" android:launchMode="singleTask" > activity> <activity android:name="customer.LoginCustomer" android:launchMode="singleTask" > activity> <activity android:name="customer.CustomerMenu" android:label="@string/title_activity_customer_menu" > activity> <activity android:name="driver.DriverMenu" > activity> application>
下面是xml布局文件中的代其中内容可进行改动
"http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FDF5E6" tools:context="customer.CustomerMenu" > "@+id/b_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > "@+id/customer_menu" android:layout_width="20dp" android:layout_height="25dp" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/customer" android:src="@drawable/transparent_mask" /> </LinearLayout> " android:layout_width=" match_parent" android:layout_height="wrap_content" android:layout_below="@id/b_layout" android:background="#FDF5E6" > "match_parent" android:layout_height="wrap_content" android:layout_marginBottom="7dp" android:layout_marginTop="7dp" android:orientation="horizontal" > "@style/customer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="快车" /> "@style/customer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="出租车" /> "@style/customer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="专车" /> "@style/customer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顺风车" /> </LinearLayout> ScrollView> map.TextureMapView android:id="@+id/mTexturemap" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/a_layout" > </com.baidu.mapapi.map.TextureMapView> " android:layout_width=" 40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_below="@id/ib_large" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:background="@drawable/small" android:src="@drawable/transparent_mask" /> @+ id/ib_large" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignLeft="@+id/ib_small" android:layout_below="@id/a_layout" android:layout_marginTop="10dp" android:background="@drawable/large" android:src="@drawable/transparent_mask" /> @+ id/ib_traffic" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignLeft="@+id/ib_loc" android:layout_below="@id/a_layout" android:layout_marginTop="10dp" android:background="@drawable/offtraffic" android:src="@drawable/transparent_mask" /> @+ id/ib_loc" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_alignTop="@+id/ib_small" android:layout_marginLeft="10dp" android:background="@drawable/loc" android:src="@drawable/transparent_mask" /> " android:layout_height=" wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:background="@drawable/rectangle_radius_fen" android:orientation="vertical" > @+ id/edit_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:layout_marginTop="15dp" android:gravity="center" android:text="现在出发" android:textSize="18sp" android:textStyle="bold" /> @+ id/start_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="7dp" android:background="@drawable/creat_normal_edittext" android:drawableLeft="@drawable/radio_red" android:drawablePadding="5dp" /> @+ id/end_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="7dp" android:background="@drawable/creat_normal_edittext" android:drawableLeft="@drawable/radio_blue" android:drawablePadding="5dp" android:hint="您要去哪儿" /> @+id/ib_mode" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignLeft="@+id/ib_small" android:layout_centerVertical="true" android:background="@drawable/mode" android:src="@drawable/transparent_mask" />
下面是主函数中的代码
需要导入的包可以在百度sdk中下载 libs文件中找到
百度地图sdk下载
只需要拷贝到自己的工程文件中就可以了
package 包名; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.map.TextureMapView; import com.baidu.mapapi.model.LatLng; import com.yd.dawm.R; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; public class CustomerMenu extends Activity implements android.view.View.OnClickListener { private TextureMapView mMapView = null; private BaiduMap mBaiduMap; // ***************************************************** private ImageButton ib_large, ib_small, ib_mode, ib_loc, ib_traffic; private EditText start_edit, end_edit; // 模式切换,正常模式 private boolean modeFlag = true; // 当前地图缩放级别 private float zoomLevel; // 定位相关 private LocationClient mLocationClient; private MyLocationListener mLocationListener; // 是否第一次定位,如果是第一次定位的话要将自己的位置显示在地图 中间 private boolean isFirstLocation = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 在使用SDK各组件之前初始化context信息,传入ApplicationContext // 注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置标题栏不可用 setContentView(R.layout.customer_menu); // 获取地图控件引用 mMapView = (TextureMapView) findViewById(R.id.mTexturemap); // 初始化控件 initView(); // 初始化地图 initMap(); // 定位 initLocation(); } private void initMap() { // 不显示缩放比例尺 mMapView.showZoomControls(false); // 不显示百度地图Logo mMapView.removeViewAt(1); // 百度地图 mBaiduMap = mMapView.getMap(); // 改变地图状态 MapStatus mMapStatus = new MapStatus.Builder().zoom(15).build(); MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); mBaiduMap.setMapStatus(mMapStatusUpdate); // 设置地图状态改变监听器 mBaiduMap.setOnMapStatusChangeListener(new OnMapStatusChangeListener() { @Override public void onMapStatusChangeStart(MapStatus arg0) { } @Override public void onMapStatusChangeFinish(MapStatus arg0) { } @Override public void onMapStatusChange(MapStatus arg0) { // 当地图状态改变的时候,获取放大级别 zoomLevel = arg0.zoom; } @Override public void onMapStatusChangeStart(MapStatus arg0, int arg1) { // TODO Auto-generated method stub } }); } private void initLocation() { // 定位客户端的设置 mLocationClient = new LocationClient(this); mLocationListener = new MyLocationListener(); // 注册监听 mLocationClient.registerLocationListener(mLocationListener); // 配置定位 LocationClientOption option = new LocationClientOption(); option.setCoorType("bd09ll");// 坐标类型 option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);// 打开Gps option.setScanSpan(1000);// 1000毫秒定位一次 option.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 mLocationClient.setLocOption(option); } private void initView() { // 地图控制按钮 ib_large = (ImageButton) findViewById(R.id.ib_large); ib_large.setOnClickListener(this); ib_small = (ImageButton) findViewById(R.id.ib_small); ib_small.setOnClickListener(this); ib_mode = (ImageButton) findViewById(R.id.ib_mode); ib_mode.setOnClickListener(this); ib_loc = (ImageButton) findViewById(R.id.ib_loc); ib_loc.setOnClickListener(this); ib_traffic = (ImageButton) findViewById(R.id.ib_traffic); ib_traffic.setOnClickListener(this); start_edit = (EditText) findViewById(R.id.start_edit); end_edit = (EditText) findViewById(R.id.end_edit); } public void onClick(View v) { switch (v.getId()) { case R.id.ib_large: if (zoomLevel < 18) { mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn()); ib_small.setEnabled(true); } else { showInfo("已经放至最大,可继续滑动操作"); ib_large.setEnabled(false); } break; case R.id.ib_small: if (zoomLevel > 6) { mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut()); ib_large.setEnabled(true); } else { ib_small.setEnabled(false); showInfo("已经缩至最小,可继续滑动操作"); } break; case R.id.ib_mode:// 卫星模式和普通模式 if (modeFlag) { modeFlag = false; mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); showInfo("开启卫星模式"); } else { modeFlag = true; mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); showInfo("开启普通模式"); } break; case R.id.ib_loc: isFirstLocation = true; showInfo("返回自己位置"); break; case R.id.ib_traffic:// 是否开启交通图 if (mBaiduMap.isTrafficEnabled()) { mBaiduMap.setTrafficEnabled(false); ib_traffic.setBackgroundResource(R.drawable.offtraffic); showInfo("关闭实时交通图"); } else { mBaiduMap.setTrafficEnabled(true); ib_traffic.setBackgroundResource(R.drawable.ontraffic); showInfo("开启实时交通图"); } break; default: break; } } @Override protected void onStart() { super.onStart(); // 开启定位 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) { mLocationClient.start(); } } @Override protected void onStop() { super.onStop(); // 关闭定位 mBaiduMap.setMyLocationEnabled(false); if (mLocationClient.isStarted()) { mLocationClient.stop(); } } @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(); } // 显示消息 private void showInfo(String str) { Toast.makeText(CustomerMenu.this, str, Toast.LENGTH_SHORT).show(); } // 自定义的定位监听 private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // 将获取的location信息给百度map MyLocationData data = new MyLocationData.Builder().accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(data); if (isFirstLocation) { // 获取经纬度 LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate status = MapStatusUpdateFactory.newLatLng(ll); // mBaiduMap.setMapStatus(status);//直接到中间 mBaiduMap.animateMapStatus(status);// 动画的方式到中间 isFirstLocation = false; showInfo("当前位置:" + location.getAddrStr()); start_edit.setText(location.getAddrStr()); } } }}
百度地图的定位不太准确
更多相关文章
- Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用
- Android沉浸式状态栏+图片背景+标题栏渐变+背景伸缩
- Android 沉浸式状态栏实现
- 基于Android的谷歌地图地理围栏功能开发
- android 电话状态的监听
- Android快速设置中添加隐藏状态栏和导航栏选项
- Android Studio开发——百度地图定位
- Android 解决沉浸式状态栏下,输入法弹出,布局不会自动调整的BUG