1、要实现高德地图的定位,首先要下载高德地图的SDK
下载地址:http://lbs.amap.com/api/android-location-sdk/download/
然后在高德地图官网上登录你注册的账号并且去控制台—–>应用管理—–>添加新key
获取到自己的key值
获取key的具体方法的官网地址 :http://lbs.amap.com/
2、通过解压得到.Jar文件并放到libs文件中,手动添加到依赖库中(右键Add—-)
3、如果你的地图SDK是3D的需要在main中创建 jniLibs 文件夹并把你解压出来的其他文件放到此处
4、开发环境已经配置好了,接下来就是敲代码了
导入高德SDK:https://lbs.amap.com/api/android-sdk/guide/create-project/android-studio-create-project
先下载所需要的SDK,然后把 jar 放进去。(只有 3D 地图才需要 so 库,其余不用放)
在工程的“ AndroidManifest.xml ”清单文件中添加key和Service
1 2 3
| <meta-data android:name="com.amap.api.v2.apikey" android:value="你申请的key" /> <service android:name="com.amap.api.location.APSService"></service>
|
添加权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <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.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
acticity_main.xml布局文件
1 2 3 4
| <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"></com.amap.api.maps.MapView>
|
MainActivity代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.LocationSource; import com.amap.api.maps.MapView; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.LatLng; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener { private AMap aMap; private MapView mapView; private AMapLocationClient mLocationClient = null; public AMapLocationClientOption mLocationOption = null; private OnLocationChangedListener mListener = null; private boolean isFirstLoc = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState); if (aMap == null) { aMap = mapView.getMap(); UiSettings settings = aMap.getUiSettings(); aMap.setLocationSource(this); settings.setMyLocationButtonEnabled(true); aMap.setMyLocationEnabled(true); } location(); } private void location() { mLocationClient = new AMapLocationClient(getApplicationContext()); mLocationClient.setLocationListener(this); mLocationOption = new AMapLocationClientOption(); mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); mLocationOption.setNeedAddress(true); mLocationOption.setOnceLocation(false); mLocationOption.setWifiActiveScan(true); mLocationOption.setMockEnable(false); mLocationOption.setInterval(2000); mLocationClient.setLocationOption(mLocationOption); mLocationClient.startLocation(); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); mLocationClient.stopLocation(); mLocationClient.onDestroy(); } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { aMapLocation.getLocationType(); aMapLocation.getLatitude(); aMapLocation.getLongitude(); aMapLocation.getAccuracy(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(aMapLocation.getTime()); df.format(date); aMapLocation.getAddress(); aMapLocation.getCountry(); aMapLocation.getProvince(); aMapLocation.getCity(); aMapLocation.getDistrict(); aMapLocation.getStreet(); aMapLocation.getStreetNum(); aMapLocation.getCityCode(); aMapLocation.getAdCode(); if (isFirstLoc) { aMap.moveCamera(CameraUpdateFactory.zoomTo(17)); aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()))); mListener.onLocationChanged(aMapLocation); StringBuffer buffer = new StringBuffer(); buffer.append(aMapLocation.getCountry() + "" + aMapLocation.getProvince() + "" + aMapLocation.getCity() + "" + aMapLocation.getProvince() + "" + aMapLocation.getDistrict() + "" + aMapLocation.getStreet() + "" + aMapLocation.getStreetNum()); Toast.makeText(getApplicationContext(), buffer.toString(), Toast.LENGTH_LONG).show(); isFirstLoc = false; } } else { Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo()); Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show(); } } } @Override public void activate(OnLocationChangedListener onLocationChangedListener) { mListener = onLocationChangedListener; } @Override public void deactivate() { mListener = null; } }
|