1.先到官网http://lbs.amap.com/api/android-location-sdk/download下载一个SDK
2.解压后
3.将解压后的文件放到libs文件下
4.复制相应的权限,服务等放到AndroidMainfest.xml中以及当然在在之前需要注册key
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sj_map">
<!--用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!--用于访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
<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.amap.api.v2.apikey"
android:value="20db771224986dabc96391fc040ae6d8"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.amap.api.location.APSService"></service>
</application>
</manifest>
6.在主线程中初始化
package com.example.sj_map;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
public class MainActivity extends AppCompatActivity {
private AMapLocationClient aMapLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aMapLocationClient = new AMapLocationClient(getApplicationContext());
AMapLocationClientOption aMapLocationClientOption = new AMapLocationClientOption();
aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
aMapLocationClientOption.setOnceLocation(true);
aMapLocationClientOption.setOnceLocationLatest(true);
aMapLocationClientOption.setNeedAddress(true);
aMapLocationClient.setLocationOption(aMapLocationClientOption);
AMapLocationListener aMapLocationListener=new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//可在其中解析amapLocation获取相应内容。
String country=amapLocation.getCountry();//国家信息
String provice=amapLocation.getProvince();//省信息
String city=amapLocation.getCity();//城市信息
String dis=amapLocation.getDistrict();//城区信息
String stree=amapLocation.getStreet();//街道信息
String floor=amapLocation.getFloor();//获取当前室内定位的楼层
Log.i("test",country+" "+provice+" "+city+" "+dis+" "+stree+" "+floor);
} else {
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
};
aMapLocationClient.setLocationListener(aMapLocationListener);
aMapLocationClient.startLocation();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(aMapLocationClient!=null){
aMapLocationClient.stopLocation();
aMapLocationClient.onDestroy();
}
}
}
如果觉得还没看懂,其实可以去官网看文档,写的很详细,也很简单
http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key