1.注册一个LBS的账号申请Key 点击打开链接
其中:Package以及SHA1的获取可见:这里
让楼主郁闷的是明明一样的key就是报错:INVALID_USER_SCODE
查找一些资料得出如下结论:
1、注册的sha1与应用签名的签名文件(包括debug.keystore)的sha1不一致。
2、Debug和release的签名文件不一样,所以最好对应注册两个不同的key,避免发布后INVALID_USER_KEY。
3、注册key时应用的包名填写不正确
4、manifest文件中mete-data标签在application标签之外。
楼主Debug跟Release的就没关注
下面上改后的代码:(需要把jks拷入app对应目录)
这样编译出来的debug版本直接用的是正式签名(release)噢
signingConfigs { release { storeFile file("key.story.jks") storePassword "123456" keyAlias "key" keyPassword "123456" } } buildTypes { debug { signingConfig signingConfigs.release } release { signingConfig signingConfigs.release minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
2.去官网下载jar...
3.配置manifest
配置AndroidManifest.xml
首先,请在application标签中声明service组件,每个app拥有自己单独的定位service。
1
|
<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
|
<!--用于进行网络定位-->
<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>
|
最后设置Key,在application标签中加入
1
2
3
|
<meta-data android:name=
"com.amap.api.v2.apikey"
android:value=
"key"
>
//开发者申请的key
</meta-data>
|
然后楼主搞了一个地图
想实现一个定位功能,楼主是在不才未能使用官网推荐的方式实现。其中还遇到过白地图的问题,原因是父控件透明度不是满状态!强烈鄙视官网的帮助!
下面贴上楼主定位库和标注实现的定位功能,先凑合用吧!
package tr.nuctech.com.tr0100.Activity; import android.graphics.Point; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.BounceInterpolator; import android.view.animation.Interpolator; 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.Projection; import com.amap.api.maps.model.BitmapDescriptor; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.CameraPosition; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import java.util.ArrayList; import java.util.List; import tr.nuctech.com.tr0100.Bean.Constants; import tr.nuctech.com.tr0100.R; import tr.nuctech.com.tr0100.Util.LBSUtils; public class MapActivity extends AppCompatActivity implements LocationSource, AMapLocationListener, AMap.OnMarkerClickListener { private MapView mapView; private AMap aMap; // private OnLocationChangedListener mListener; //初始化定位对象 private AMapLocationClient locationClient = null; private AMapLocationClientOption locationOption = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); mapView = (MapView) findViewById(R.id.id_map); mapView.onCreate(savedInstanceState);// 此方法必须重写 init(); //初始化定位模块 locationClient = new AMapLocationClient(this); locationOption = new AMapLocationClientOption(); // 设置定位模式为 locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); // 设置定位监听 locationClient.setLocationListener(this); // //只有持续定位设置定位间隔才有效,单次定位无效 // layoutInterval.setVisibility(View.VISIBLE); //设置为不是单次定位 locationOption.setOnceLocation(true); // 设置定位参数 locationClient.setLocationOption(locationOption); new Thread(){ @Override public void run() { super.run(); // 启动定位 try { Thread.sleep(2000); locationClient.startLocation(); mHandler.sendEmptyMessage(LBSUtils.MSG_LOCATION_START); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } /** * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); aMap.setLocationSource(this);// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false // 设置定位的类型为定位模式,参见类AMap。 aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); //marker // aMap.setOnMarkerDragListener(this);// 设置marker可拖拽事件监听器 // aMap.setOnMapLoadedListener(this);// 设置amap加载成功事件监听器 aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器 // aMap.setOnInfoWindowClickListener(this);// 设置点击infoWindow事件监听器 // aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式 } } Handler mHandler = new Handler(){ public void dispatchMessage(android.os.Message msg) { switch (msg.what) { case LBSUtils.MSG_LOCATION_START: Toast.makeText(MapActivity.this,"正在定位...",Toast.LENGTH_SHORT).show(); break; //定位完成 case LBSUtils.MSG_LOCATION_FINISH: AMapLocation loc = (AMapLocation)msg.obj; String result = LBSUtils.getLocationStr(loc); Toast.makeText(MapActivity.this,result,Toast.LENGTH_SHORT).show(); // tvReult.setText(result); break; case LBSUtils.MSG_LOCATION_STOP: Toast.makeText(MapActivity.this,"定位停止",Toast.LENGTH_SHORT).show(); break; default: break; } }; }; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onDestroy() { super.onDestroy(); if (null != locationClient) { /** * 如果AMapLocationClient是在当前Activity实例化的, * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy */ locationClient.onDestroy(); locationClient = null; locationOption = null; } } @Override protected void onPause() { super.onPause(); } @Override protected void onResume() { super.onResume(); } @Override protected void onStart() { super.onStart(); } @Override public void onLocationChanged(AMapLocation aMapLocation) { //定位回调 Toast.makeText(this,"onLocationChanged",Toast.LENGTH_SHORT).show(); if (this != null && aMapLocation != null) { if (aMapLocation != null && aMapLocation.getErrorCode() == 0) { // this.onLocationChanged(aMapLocation);// 显示系统小蓝点 LatLng postion=new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude()); CameraPosition position=new CameraPosition(postion , 18, 0, 30); aMap.moveCamera(CameraUpdateFactory.newCameraPosition(position)); initMark(postion); } else { String errText = "定位失败," + aMapLocation.getErrorCode()+ ": " + aMapLocation.getErrorInfo(); Toast.makeText(this,"errText",Toast.LENGTH_SHORT).show(); } } // if (null != aMapLocation) { // Message msg = mHandler.obtainMessage(); // msg.obj = aMapLocation; // msg.what = LBSUtils.MSG_LOCATION_FINISH; // mHandler.sendMessage(msg); // } } @Override public void activate(OnLocationChangedListener onLocationChangedListener) { Toast.makeText(this,"activate",Toast.LENGTH_SHORT).show(); } @Override public void deactivate() { Toast.makeText(this,"deactivate",Toast.LENGTH_SHORT).show(); } private void initMark(LatLng latLng) { ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); giflist.add(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)); giflist.add(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_RED)); giflist.add(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)); MarkerOptions markerOption1 = new MarkerOptions().anchor(0.5f, 0.5f) .position(latLng).title("同方威视") .snippet("Creating a safer world").icons(giflist) .draggable(true).period(50); ArrayList<MarkerOptions> markerOptionlst = new ArrayList<MarkerOptions>(); markerOptionlst.add(markerOption1); List<Marker> markerlst = aMap.addMarkers(markerOptionlst, true); } @Override public boolean onMarkerClick(Marker marker) { jumpPoint(marker); return false; } /** * marker点击时跳动一下 */ public void jumpPoint(final Marker marker) { final Handler handler = new Handler(); final long start = SystemClock.uptimeMillis(); Projection proj = aMap.getProjection(); Point startPoint = proj.toScreenLocation(Constants.XIAN); startPoint.offset(0, -100); final LatLng startLatLng = proj.fromScreenLocation(startPoint); final long duration = 1500; final Interpolator interpolator = new BounceInterpolator(); handler.post(new Runnable() { @Override public void run() { long elapsed = SystemClock.uptimeMillis() - start; float t = interpolator.getInterpolation((float) elapsed / duration); double lng = t * Constants.XIAN.longitude + (1 - t) * startLatLng.longitude; double lat = t * Constants.XIAN.latitude + (1 - t) * startLatLng.latitude; // marker.setPosition(new LatLng(lat, lng)); marker.setPosition(marker.getPosition()); if (t < 1.0) { handler.postDelayed(this, 16); } } }); } }