百度地图定位
Posted 可爱的小阿轩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度地图定位相关的知识,希望对你有一定的参考价值。
百度地图定位
作业4
要求
根据官方文档,开发百度地图定位APP,显示出当前位置坐标。
源代码
MainActivity:
public class MainActivity extends Activity
private MapView mapView;
private BaiduMap baiduMap;
private LocationManager locationManager;
private String provider;
private boolean isFirstLocate =true;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map_view);
baiduMap=mapView.getMap();
setLovationManager();
baiduMap.setMyLocationEnabled(true);
private void setLovationManager()
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
List<String> providerList=locationManager.getProviders(true);
if(providerList.contains(LocationManager.GPS_PROVIDER))
provider=LocationManager.GPS_PROVIDER;
else if(providerList.contains(LocationManager.NETWORK_PROVIDER))
provider=LocationManager.NETWORK_PROVIDER;
else
Toast.makeText(this,"没有可用的位置提供器",Toast.LENGTH_SHORT).show();
return;
Location location=locationManager.getLastKnownLocation(provider);
if(location!=null)
navigateTo(location);
locationManager.requestLocationUpdates(provider,5000,5,locationListener);
private void navigateTo(Location location)
if(isFirstLocate)
LatLng ll=new LatLng(location.getLatitude(),location.getLongitude());
MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
update=MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocate=false;
MyLocationData.Builder locationBuilder=new MyLocationData.Builder();
locationBuilder.latitude(location.getLatitude());
locationBuilder.longitude(location.getLongitude());
MyLocationData locationData=locationBuilder.build();
baiduMap.setMyLocationData(locationData);
LocationListener locationListener =new LocationListener()
@Override
public void onLocationChanged(Location location)
if(locationManager!=null)
navigateTo(location);
@Override
public void onStatusChanged(String s, int i, Bundle bundle)
@Override
public void onProviderEnabled(String s)
@Override
public void onProviderDisabled(String s)
;
@Override
protected void onDestroy()
super.onDestroy();
mapView.onDestroy();
if(locationManager!=null)
locationManager.removeUpdates(locationListener);
baiduMap.setMyLocationEnabled(false);
@Override
protected void onPause()
super.onPause();
mapView.onPause();
@Override
protected void onResume()
super.onResume();
mapView.onResume();
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<com.baidu.mapapi.map.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
</com.baidu.mapapi.map.MapView>
</LinearLayout>
Androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.baidumaptest2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="lPdICf3mOjPpwXsUzv8Omgec"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<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.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<!-- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" >
</uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 来电消音 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
</manifest>
界面
以上是关于百度地图定位的主要内容,如果未能解决你的问题,请参考以下文章