高德地图

高德地图

坑:1.模拟器定位不了

     2.发布版的SHA1只能打包才能用,开发版用调试版安全码SHA1

     3.获取调试版安全码SHA1要去C:\Users\admin\.android,打开控制台输入keytool -list -v -keystore debug.keystore,默认密码           android,输的密码控制台不显示,输完密码直接回车就出来了

     4.定位没有在清单文件中加定位服务

1.地图布局和清单文件配置


<!--用于进行网络定位-->
<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>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>


<service android:name="com.amap.api.location.APSService"/>//定位服务


<meta-data android:name="com.amap.api.v2.apikey" android:value="key"/>//开发者申请的key  
 <com.amap.api.maps2d.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

2.地图的生命周期

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectlocation);
        mMapView = (MapView) findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);// 此方法须覆写,虚拟机需要在很多情况下保存地图绘制的当前状态。
    }

@Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mMapView.onDestroy();
    }


    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

 

 3.权限

 if (Build.VERSION.SDK_INT >= 23) {

            String[] permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.READ_PHONE_STATE
            };
            for (String str : permissions) {
                if (checkSelfPermission(str) == PackageManager.PERMISSION_DENIED) {
                    requestPermissions(permissions, 0);
                }
            }
        }

 4.权限回调

 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //继续操作权限
    }

5.地图SDK定位前的准备 

mMapView = (MapView) findViewById(R.id.mapView);
 if (aMap == null) {
            aMap = mMapView.getMap();//获取地图
        }
       // aMap.animateCamera(CameraUpdateFactory.zoomTo(19),null);//地图缩放级别
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(getLatitude(),     getLongitude()), 19f));//以当前位置进行缩放
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory
                .fromResource(R.drawable.gps_point));//我的位置图标
        myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));//地图定位后会自行显示一个蓝色的圆
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));//圈,设置为透明消失
        myLocationStyle.showMyLocation(true);//显示我的位置
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);//单次定位显示
        aMap.setMyLocationStyle(myLocationStyle);

        aMap.getUiSettings().setMyLocationButtonEnabled(false);//关闭地图右上角位置图标
        aMap.getUiSettings().setZoomControlsEnabled(false);//关闭放大缩小按钮
        aMap.getUiSettings().setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);
//小意外,自定义位置按钮和logo放在一起会把logo挤出地图
        aMap.setLocationSource(this);//设置定位监听
        aMap.setMyLocationEnabled(true);//显示定位层并可触发,默认false

  5.1地图移动

aMap.animateCamera(CameraUpdateFactory.changeLatLng(latLng));//地图移动有动画,平移

aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));//地图移动无动画,瞬移

6.地图的定位监听要实现 LocationSource接口,会有3个方法

7.定位SDK正式定位前的设置

 @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        //官方建议getApplicationContext()
        mlocationClient = new AMapLocationClient(getApplicationContext());
        mlocationClient.setLocationListener(this);//设置监听回调,没有这个地图显示不了
        //初始化定位参数
        AMapLocationClientOption clientOption = new AMapLocationClientOption();
        //设置高精度定位,还有省电模式和设备模式
clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        clientOption.setNeedAddress(true);//不打开后面获取不了Address
        clientOption.setOnceLocation(true);//单次定位
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        clientOption.setMockEnable(false);
        //设置定位间隔
        clientOption.setInterval(2000);
        mlocationClient.setLocationOption(clientOption);
        mlocationClient.startLocation();//开始定位
    }
 @Override
    public void deactivate() {//停止定位
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }
  @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null && aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功完成回调
                searchLatlonPoint = new LatLonPoint(aMapLocation.getLatitude(),              aMapLocation.getLongitude());//获取当前位置经纬度
                String city = aMapLocation.getCity();//获取当前位置的城市信息等...
                doSearchQuery(searchLatlonPoint);//获取当前位置的poi
                mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            } else {
                //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }

8.获取当前位置的POI前的准备

POI默认三种类型:"餐饮服务","商务住宅","生活服务".

POI搜索类型共分为以下20种:汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施

 private void doSearchQuery(LatLonPoint latLonPoint) {
        String type = "餐饮服务|购物服务|生活服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|金融保险服务|公司企业|公共设施";
        PoiSearch.Query query = new PoiSearch.Query("", type, city);//第一个表示搜索字符串,第二个参数表示POI搜索类型,二者选填其一,第三个表示POI搜索区域,
        //可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索
        query.setPageSize(30);//返回数据的数量
        query.setPageNum(1);//返回的第几页
        PoiSearch poisearch = new PoiSearch(this, query);
        poisearch.setOnPoiSearchListener(this);//设置监听
        poisearch.setBound(new PoiSearch.SearchBound(latLonPoint, 1000));//当前位置方圆1000米内的POI数据,latLonPoint是经纬度
        poisearch.searchPOIAsyn();//异步查询
    }

9.POI查询结果回调


    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (poiResult != null && poiResult.getPois().size() > 0) {
                List<PoiItem> poiItems = poiResult.getPois();
                  poiList.clear();
                poiList.addAll(poiItems);
                poiListAdapter.notifyDataSetChanged();
            } else {
                Toast.makeText(this, "无搜索结果", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "搜索失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

 10.地图添加标记

 private void addMarker(LatLng latLng) {
        if (marker != null) {
            String tag = (String) marker.getObject();
            if (tag.equals("tag")) {//自己设置的,用于判断是否属于要移除的标记
                marker.remove();//移除
            }
        }
        MarkerOptions options = new MarkerOptions().icon(BitmapDescriptorFactory
                .fromResource(R.drawable.poi_marker_pressed))
                .position(latLng)//标记的经纬度
                .draggable(false);//是否可以拖拽
        marker = aMap.addMarker(options);

        aMap.animateCamera(CameraUpdateFactory.changeLatLng(latLng));//地图移动到标记位置
        marker.setObject("tag");//自己设置的
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值