首先,分别获得应用程序发布版和调试版安全码SHA1,获得后在高德地图注册开发者账号,获得APPKey。 其次,将定位SDK压缩包中的.jar包放入项目的libs目录下,将解压得到的.so文件中的.so文件。放到在main目录下创建的jniLibs目录下。 再次,进入主题,在AndroidManifest.xml中配置权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.access_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />高德地图 配置APPkey
<meta-data android:name="com.amap.api.v2.apikey" android:value="KEY" />配置Service
<service android:name="com.amap.api.location.APSService" />在布局文件中添加地图控件
<!-- 引入布局文件 --> <com.amap.api.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" />在Activity中写代码
import android.app.Activity;import android.graphics.BitmapFactory;import android.os.Bundle;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.MapView;import com.amap.api.maps.model.BitmapDescriptor;import com.amap.api.maps.model.BitmapDescriptorFactory;import com.amap.api.maps.model.LatLng;import com.amap.api.maps.model.MarkerOptions;import java.text.SimpleDateFormat;import java.util.Date;public class LocationActivity extends Activity { PRivate MapView mapView; private AMap aMap; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; /** * 声明定位回调监听器 */ public AMapLocationListener mLocationListener = new AMapLocationListener() { @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();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。 amapLocation.getCountry();//国家信息 amapLocation.getProvince();//省信息 amapLocation.getCity();//城市信息 amapLocation.getDistrict();//城区信息 amapLocation.getStreet();//街道信息 amapLocation.getStreetNum();//街道门牌号信息 amapLocation.getCityCode();//城市编码 amapLocation.getAdCode();//地区编码 amapLocation.getAoiName();//获取当前定位点的AOI信息 lat = amapLocation.getLatitude(); lon = amapLocation.getLongitude(); // 设置当前地图显示为当前位置 LatLng latLng=new LatLng(lat, lon); aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 19)); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title("当前位置"); markerOptions.snippet(amapLocation.getProvince()+ amapLocation.getDistrict()+amapLocation.getAoiName()); markerOptions.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_RED)); markerOptions.visible(true); aMap.addMarker(markerOptions); } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } } }; //声明mLocationOption对象 public AMapLocationClientOption mLocationOption = null; private double lat; private double lon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState);//必须要写 //初始化定位 mLocationClient = new AMapLocationClient(getapplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); init(); } /** * * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); } setUpMap(); } /** * 配置定位参数 */ private void setUpMap() { //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否只定位一次,默认为false mLocationOption.setOnceLocation(false); //设置是否强制刷新WIFI,默认为强制刷新 mLocationOption.setWifiActiveScan(true); //设置是否允许模拟位置,默认为false,不允许模拟位置 mLocationOption.setMockEnable(false); //设置定位间隔,单位毫秒,默认为2000ms mLocationOption.setInterval(2000); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); } /** * 方法必须重写 */ @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必须重写 */ @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onStop() { super.onStop(); mLocationClient.stopLocation();//停止定位 } /** * 方法必须重写 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必须重写 */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); mLocationClient.onDestroy();//销毁定位客户端。 }}这样一个简单的定位就完成。
新闻热点
疑难解答