高德地圖對接

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”錯誤的解決

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章