利用百度地图sdk定位 要注意的
最近有个小项目需要进行一些定位的操作,找来找去,觉得百度做的还是比较好的。
就记录一点,
MainActivity.java
public class MainActivity extends Activity { public LocationClient mLocationClient = null; public LocationClientOption option; public BDLocationListener myListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"地址:"+ arg0.getAddrStr()+"--"+arg0.getCity()+"--"+arg0.getLatitude()+"---"+arg0, Toast.LENGTH_LONG).show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 option=new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式 option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms option.setIsNeedAddress(true);//返回的定位结果包含地址信息 option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向 option.setAddrType("all"); mLocationClient.setLocOption(option); mLocationClient.registerLocationListener( myListener ); //注册监听函数 } public void getLocation(View view){ mLocationClient.start(); } }
mainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.locationtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <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.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > </uses-permission> <uses-permission android:name="android.permission.READ_LOGS" > </uses-permission> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > <intent-filter> <action android:name="com.baidu.location.service_v2.2" > </action> </intent-filter> </service> <!-- meta-data需要写在application中 --> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="............." />//自己的key <activity android:name=".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> </application> </manifest>
一开始没有设置locationClientOption导致使用location.getAddrStr()的时候获取的结果都是null。
没有设置的话,所有文字的结果都是null。。记住这一点就行。其他按着配置就行。
相关推荐
magvwiz 2020-09-26
mickeychan 2020-09-17
rungod 2020-09-16
xiaozhukuaitui 2020-09-05
小西0 2020-09-05
xiaozhukuaitui 2020-08-25
龙心尘 2020-08-25
rungod 2020-08-22
waiwaiLILI 2020-07-20
shilongdred 2020-06-28
人工智能快报 2020-06-09
shilongdred 2020-06-08
id=3&hmsr=%E5%BC%80%E5%8F%91%E8%80%85-AI%E5%AD%A6%E4%B9%A0%E8%B7%AF%E7%BA%BF&hmpl=&hmcu=&hmkw=&hmci=
zmosquito 2020-06-02
nextwhy 2020-05-20
fengyeezju 2020-04-20
etzt 2020-04-16