1、对接
1)初始化
MapView mapView = (MapView) findViewById(R.id.map2);
mapView.onCreate(savedInstanceState);
aMap = mapView.getMap();
2)移动地图位置
aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
aMap.moveCamera(CameraUpdateFactory.zoomTo(13));
3)定位
//定位客户端
AMapLocationClient mlocationClient;
//定位配置信息
AMapLocationClientOption mLocationOption ;
//位置监听
AMapLocationListener mListener=new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {//位置变化
if (null != location) {//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if(location.getErrorCode() == 0){
mlocationClient.stopLocation();
mBinding.location.setText(location.getStreet().equals("")? location.getAddress():location.getStreet());
}else{
mBinding.location.setText("--");
Log.d(TAG,"location code:"+location.getErrorCode());
}
}else{
mBinding.location.setText("--");
Log.d(TAG,"location failure");
}
}
} ;
//定位配置初始化
void showLocation(){
mLocationOption = new AMapLocationClientOption();
//设置高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(10000);
mLocationOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mLocationOption.setGpsFirst(true);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mLocationOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
mLocationOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
mLocationOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
//AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mLocationOption.setSensorEnable(true);//可选,设置是否使用传感器。默认是false
mLocationOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
mLocationOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
mlocationClient = new AMapLocationClient(MainActivity.this);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
//设置定位监听
mlocationClient.setLocationListener(mListener);
//开始定位
mlocationClient.startLocation();
}
4、解析指定位置
//根据Location 获取AMapLocation
public AMapLocation fromGpsToAmap(Location location) {
AMapLocation aMapLocation = new AMapLocation(location);
try {
CoordinateConverter converter = new CoordinateConverter(this);
converter.from(CoordinateConverter.CoordType.GPS);
try {
converter.coord(new DPoint(location.getLatitude(), location.getLongitude()));
DPoint desLatLng = converter.convert();
aMapLocation.setLatitude(desLatLng.getLatitude());
aMapLocation.setLongitude(desLatLng.getLongitude());
SPUtils.putString(MainActivity.this, ElcoUrl.LATITUDE, desLatLng.getLatitude() + "");
SPUtils.putString(MainActivity.this, ElcoUrl.LONGITUDE, desLatLng.getLongitude() + "");
} catch (Exception e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
return aMapLocation;
}
//根据AMapLocation获取地点
public void getadress(AMapLocation location){
try {
//地点搜索
GeocodeSearch geocoderSearch = new GeocodeSearch(this);
//设置查询参数:三个参数依次为坐标,范围多少米,坐标系
RegeocodeQuery regeocodeQuery = new RegeocodeQuery(
new LatLonPoint(location.getLatitude(), location.getLongitude()), 200, GeocodeSearch.AMAP);
//设置查询结果监听
geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
//根据坐标获取地址信息调用
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
try {//搜索结果非空,以下判断是为了保证数据有效可控
if(regeocodeResult != null) {
String s = regeocodeResult.getRegeocodeAddress().getFormatAddress();
String value = null;
if (regeocodeResult.getRegeocodeAddress().getTownship().equals("") && s.equals(""))
value = "--";
else if(regeocodeResult.getRegeocodeAddress().getTownship().equals("")){
value = s;
}else {
int startNum = regeocodeResult.getRegeocodeAddress().getFormatAddress().indexOf(regeocodeResult.getRegeocodeAddress().getTownship());
value = regeocodeResult.getRegeocodeAddress().getFormatAddress().substring(startNum, regeocodeResult.getRegeocodeAddress().getFormatAddress().length());
}
mBinding.location.setText(value);
Log.i(TAG,"坐标获取地址:"+value);
}else{//没有获取到有效地址
mBinding.location.setText("--");
}
}catch(Exception e){//解析出错
e.printStackTrace();
mBinding.location.setText("--");
}
}
//根据地址获取坐标信息时调用
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
});
//发起异步查询请求
geocoderSearch.getFromLocationAsyn(regeocodeQuery);
}catch (Exception e){
e.printStackTrace();
}
}
4)描点
1】弹窗描点
aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
String snippet = marker.getSnippet();
String title = marker.getTitle();
View infoWindow = LayoutInflater.from(MainActivity.this).inflate(
R.layout.layout_button, null);
TextView infowindow_text = (TextView) infoWindow.findViewById(R.id.infowindow_title);
lock = (TextView) infoWindow.findViewById(R.id.lock);
lock.setEnabled(false);
lock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return infoWindow;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
});
//创建标记点
markerOption = new MarkerOptions();
markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),R.mipmap.location)))
markerOption.position(latLng)
.draggable(true);
Marker marker = aMap.addMarker(markerOption);
marker.showInfoWindow();
2】重绘图标描点
自定义View作为map
View inflate = getLayoutInflater().inflate(R.layout.layout_inforwindow_normal, null);
((TextView) inflate.findViewById(R.id.title)).setText(modle.getBianHao());
((TextView) inflate.findViewById(R.id.snippet)).setText(modle.getIMEI());
Bitmap bitmap = convertViewToBitmap(inflate);
// 创建标记点
markerOption = new MarkerOptions();
markerOption.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
markerOption.position(latLng)
.draggable(true);
Marker marker = aMap.addMarker(markerOption);
marker.showInfoWindow();
锚点监听
aMap.setOnMarkerClickListener(this);
锚点移除
private void removeMarker(String tag){
//获取地图上所有Marker
List<Marker> mapScreenMarkers = aMap.getMapScreenMarkers();
for (int i = 0; i < mapScreenMarkers.size(); i++) {
Marker marker = mapScreenMarkers.get(i);
if(marker.getObject()!=null&&
marker.getObject().toString().equals(name)){
marker.remove();//移除当前Marker
}//需要注意,地图上默认的定位点也是一个marker,此时会造成getObject()的空指针异常
}
aMap.reloadMap();//刷新地图,替换原本的invalidate();
}
锚点的替换
实际使用中在增驾和移除的基础上做了新的封装逻辑,以确保地图上的点是唯一的,且是最新的
//用于缓存marker和标记物的关系
private HashMap<Marker, Entity> mMarkerEntityHashMap;
private void resetMarker(Entity entity){
List<Marker> mapScreenMarkers = aMap.getMapScreenMarkers();
boolean b=true;//用于判断地图上是否由此点的标记
for (int i = 0; i < mapScreenMarkers.size(); i++) {
Marker marker = mapScreenMarkers.get(i);
if(marker.getObject()!=null && marker.getObject().toString().equals(entity.getSite_id())){//已绘制
b =false;
//获取标记物
Entity nowEntity = mMarkerEntityHashMap.get(marker);
if(!now.isSame(entity)){//通过自定义函数isSame判断两个Entity实例的值是否相等
marker.remove();//不相等时移除当前Marker
addMarkersToMap(entity);//并添加新的点
}
}
}
if(b){//未绘制此点
addMarkersToMap(entity);
}
aMap.reloadMap();//刷新地图,替换原本的invalidate();
}
1、卡顿
出现:小米手机
联网状况:连接wifi,
04-28 08:51:39.959 12414-12491/com.elco.guizhouyidong E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
04-28 08:51:46.253 12414-12421/com.elco.guizhouyidong I/art: Thread[2,tid=12421,WaitingInMainSignalCatcherLoop,Thread*=0xaec0f000,peer=0x12cf50a0,”Signal Catcher”]: reacting to signal 3
04-28 08:51:46.379 12414-12421/com.elco.guizhouyidong I/art: Wrote stack traces to ‘/data/anr/traces.txt’
ANDROID “call to opengl es api with no current context”错误的解决