高德地图简单地图及定位

    xiaoxiao2021-03-25  162

    首先声明下,高德地图自带的demo已经很好的将开发需要的准备工作及开发流程讲解清楚,而这里我是觉得他的demo里面的代码过于庞大,所以自己打算一点点拆分他的项目,在这里重新规划下。

    【高德完整示例代码】(http://lbs.amap.com/api/android-sdk/download/)

    1、新建项目

    这里我是用android studio2.1.2的版本作开发,最低适配4.3的版本。

    2、配置API Key

    按照官网配置

    按照官网导入jar包、配置xml

    3、xml代码

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.gdmap.mymapdemo.MainActivity"> <android.support.v4.widget.DrawerLayout android:id="@+id/main_drawer" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/main_rl" android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps2d.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" /> <RadioGroup android:id="@+id/check_language" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/border_bg" android:paddingRight="4dp" android:orientation="vertical"> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_toRightOf="@id/check_language" android:layout_margin="10dp" android:layout_alignTop="@id/map"> <Button android:id="@+id/basicmap" android:layout_width="80dp" android:layout_height="40dp" android:gravity="center" android:background="@drawable/btn_map_poi" android:textColor="#000000" android:textSize="13sp" android:text="标准地图"/> <Button android:id="@+id/rsmap" android:layout_width="80dp" android:layout_height="40dp" android:gravity="center" android:background="@drawable/btn_map_poi" android:textColor="#000000" android:textSize="13sp" android:text="卫星地图"/> </LinearLayout> <TextView android:id="@+id/location_errInfo_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:background="@color/red" android:textColor="@color/darkgrey" android:text="TextView" android:visibility="gone"/> </RelativeLayout> <LinearLayout android:id="@+id/main_right_li" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="right"> </LinearLayout> </android.support.v4.widget.DrawerLayout>

    4、java代码

    package com.gdmap.mymapdemo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioGroup; 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.maps2d.AMap; import com.amap.api.maps2d.LocationSource; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.MyLocationStyle; public class MainActivity extends Activity implements OnClickListener, LocationSource,AMapLocationListener { private MapView mapView; private AMap aMap; private Button basicmap; private Button rsmap; private OnLocationChangedListener mListener; private AMapLocationClient mlocationClient; private AMapLocationClientOption mLocationOption; private RadioGroup mRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState);// 此方法必须重写 init(); } /** * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); setUpMap(); } basicmap = (Button)findViewById(R.id.basicmap); basicmap.setOnClickListener(this); rsmap = (Button)findViewById(R.id.rsmap); rsmap.setOnClickListener(this); mRadioGroup = (RadioGroup) findViewById(R.id.check_language); mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { } }); } /** * 设置一些amap的属性 */ private void setUpMap() { // 自定义系统定位小蓝点 MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory .fromResource(R.drawable.location_marker));// 设置小蓝点的图标 myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色 myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色 // myLocationStyle.anchor(int,int)//设置小蓝点的锚点 myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细 aMap.setMyLocationStyle(myLocationStyle); aMap.setLocationSource(this);// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 // 设置为true表示显示定位层并可触发定位, // false表示隐藏定位层并不可触发定位,默认是false aMap.setMyLocationEnabled(true); // aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); } /** * 方法必须重写 */ @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必须重写 */ @Override protected void onPause() { super.onPause(); mapView.onPause(); } /** * 方法必须重写 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必须重写 */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); if(mlocationClient!=null){ mlocationClient.onDestroy(); } } /** * 定位成功后回调函数 */ @Override public void onLocationChanged(AMapLocation amapLocation) { if (mListener != null && amapLocation != null) { if (amapLocation != null && amapLocation.getErrorCode() == 0) { mListener.onLocationChanged(amapLocation);// 显示系统小蓝点 } else { String errText = "定位失败," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo(); Log.e("AmapErr",errText); } } } /** * 激活定位 */ @Override public void activate(LocationSource.OnLocationChangedListener listener) { mListener = listener; if (mlocationClient == null) { mlocationClient = new AMapLocationClient(this); mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(this); //设置为高精度定位模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 mlocationClient.startLocation(); } } /** * 停止定位 */ @Override public void deactivate() { mListener = null; if (mlocationClient != null) { mlocationClient.stopLocation(); mlocationClient.onDestroy(); } mlocationClient = null; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.basicmap: aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式 break; case R.id.rsmap: aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式 break; } } }

    运行效果:

    ok,其他注意事项如添加权限及apikey配置详见官网。

    转载请注明原文地址: https://ju.6miu.com/read-5472.html

    最新回复(0)