添加 AMap_Location jar包,在AndroidManifest 添加权限,并在 application下 添加申请到的 key
这里还可以添加service,不过我发现不添加也没有影响
<service android:name="com.amap.api.location.APSService"></service>
之后就可以使用定位了,定位得到的 AMapLocation 对象 直接tostring就能得到所有的定位信息。
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationListener; public class LocationActivity extends AppCompatActivity { private TextView tv; public AMapLocationClient LocationClient = null; public AMapLocationListener LocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { tv.setText(aMapLocation.toString()); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); tv = (TextView) findViewById(R.id.tv); LocationClient = new AMapLocationClient(getApplicationContext()); LocationClient.setLocationListener(LocationListener); } public void btn(View view) { switch (view.getId()) { case R.id.btn1: LocationClient.startLocation(); break; case R.id.btn2: LocationClient.stopLocation(); break; } } }
百度定位的sdk 除了jar包还有so库,所以添加的东西要比高德多。
在AndroidManifest 添加权限,并在 application下 添加申请到的 key,而百度这里必须添加service
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
之后就可以定位了
这里比高德稍微麻烦一点,还要配置定位SDK参数 LocationClientOption,不然最多只能拿到经纬度,而获取不到具体信息
而且获取到的 BDLocation 对象,不能tostring,只能按需求取数据
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; public class BaiduActivity extends AppCompatActivity { private TextView tv; private LocationClient locationClient = null; private BDLocationListener bdLocationListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation bdLocation) { if (null != bdLocation && bdLocation.getLocType() != BDLocation.TypeServerError) { tv.setText(bdLocation.getLocType() + bdLocation.getAddrStr() + " " + bdLocation.getBuildingName() + " " + bdLocation.getDistrict() + " " + bdLocation.getFloor() + " " + bdLocation.getStreet() + " " + bdLocation.getTime() ); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baidu); tv = (TextView) findViewById(R.id.tv); locationClient = new LocationClient(getApplicationContext()); locationClient.registerLocationListener(bdLocationListener); LocationClientOption clientOption = new LocationClientOption(); clientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); clientOption.setCoorType("bd09ll"); clientOption.setIsNeedAddress(true); clientOption.setIsNeedLocationDescribe(true); clientOption.setIsNeedLocationPoiList(true); locationClient.setLocOption(clientOption); } public void btn(View view) { switch (view.getId()) { case R.id.btn1: locationClient.start(); break; case R.id.btn2: locationClient.stop(); break; } } }
简单对比
Sdk大小 |
难易 |
模拟器 |
文档 |
室内 |
位置提醒 |
同步异步 |
|
百度 |
511 KB |
适中 |
支持 |
详细 |
支持 |
支持 |
异步 |
高德 |
259 KB |
简单 |
貌似不支持 |
详细 |
有另外的室内定位sdk |
不支持 |
开启定位异步 关闭定位同步 |
值得一提的是 高德在直接LocationClient.stopLocation() 时,会明显卡顿,说明阻塞了主线程,还是开新线程关闭好
获取key的方法
打开cmd,定位到.android文件夹下,输入cd .android
keytool -list -v -keystore C:\AndroidProject\demonkey.jks
然后输入密码 复制sha1就行了
时间: 2024-10-12 17:48:38