天天看點

第一次接觸高德,初步內建需要的功能

1.首先導入高德地圖的sdk

2.編輯manifest的mete—data

<meta-data
    android:name="com.amap.api.v2.apikey"
    android:value="***************************" />      

3.Application中實作接口:

public class MyApplication extends Application implements AMapLocationListener {
    private static MyApplication myApplication;
    private int screenHeight;
    private int screenWidth;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;      
4.設定高德功能      
private void amapInit() {
    //初始化定位
    mLocationClient = new AMapLocationClient(getApplicationContext());
    //設定定位回調監聽
    mLocationClient.setLocationListener(this);
    //定位參數配置
    mLocationOption = new AMapLocationClientOption();
    //低功耗模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
    //定位周期30s
    mLocationOption.setInterval(30000);
    mLocationClient.setLocationOption(mLocationOption);
    //啟動定位
    mLocationClient.startLocation();

}      
5.建立MapActivity      
初始化map        
mMapView  = (MapView) findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);      
private void maiInit() {

        if (aMap == null) {
            aMap = mMapView.getMap();
            //縮放級别≥17級時,地圖上可以顯示室内地圖
            // 縮放級别≥18級時,不僅可以看到室内地圖效果,還允許操作切換樓層,顯示精細化室内地圖。
            aMap.showIndoorMap(true);
//            aMap.setInfoWindowAdapter(new MInfoWindowAdapter(MapActivity.this));
            mUiSettings = aMap.getUiSettings();
            mUiSettings.setZoomControlsEnabled(false);
            mUiSettings.setCompassEnabled(true);

//           
            //設定marker點選事件
            aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    if(marker.isInfoWindowShown()){
                        marker.hideInfoWindow();
                    }else {
                        marker.showInfoWindow();

                    }
                    return true;
                }
            });
            //詳情點選事件
            aMap.setOnInfoWindowClickListener(new AMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {

                }
            });
        }

        //添加圓
        LatLng latLng = new LatLng(MyApplication.getInstance().getLatitude(), MyApplication.getInstance().getLongitude());
        aMap.addCircle(new CircleOptions().
                center(latLng).
                radius(3000).
                fillColor(Color.argb(30,106, 120, 140)).
                strokeColor(Color.rgb(106, 120, 140)).
                strokeWidth(2));

        //初始化設定縮放級别
//        aMap.moveCamera(CameraUpdateFactory.zoomTo(14));
    }      
7.設定定位以及自定義的marker和infowindow
//參數依次是:視角調整區域的中心點坐标、希望調整到的縮放級别、俯仰角0°~45°(垂直與地圖時為0)、偏航角 0~360° (正北方為0)
                CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(MyApplication.getInstance().getLatitude(), MyApplication.getInstance().getLongitude()),18,0,0));
                gaodeMap = new MyStyleGDMap(MapActivity.this);
                aMap.setInfoWindowAdapter(gaodeMap);
                aMap.animateCamera(mCameraUpdate, 2000, new AMap.CancelableCallback() {
                    @Override
                    public void onFinish() {

                        for(int i = 0; i < allMapInfo.size(); i++){
                            LatLng latLng1 = new LatLng(Double.parseDouble(allMapInfo.get(i).getLatitude()), Double.parseDouble(allMapInfo.get(i).getLongitude()));
                            MarkerOptions markerOption = new MarkerOptions();
                            markerOption.position(latLng1);
                            markerOption.title(allMapInfo.get(i).getName()).snippet(allMapInfo.get(i).getAddress());
                            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                                    .decodeResource(getResources(),R.mipmap.map_shangchang)));
                            Marker marker = aMap.addMarker(markerOption);
                            marker.setObject(allMapInfo.get(i).getPhotoUrl());
                            String path = (String) marker.getObject();
                        }
//
                    }

                    @Override
                    public void onCancel() {

                    }
                });