1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android之高德地图定位SDK集成及地图功能实现

Android之高德地图定位SDK集成及地图功能实现

时间:2021-07-09 02:54:17

相关推荐

Android之高德地图定位SDK集成及地图功能实现

一:百度高德官方网站,然后去创建应用、

网址:/

1.找到控制台创建一个应用、

2.添加key名称,注意命名规范,还有就是下面的SHA1和包名、

3.点击右边的Gradle再选择signingReport下面会有个命令,稍等几分钟得到SHA1.

4.添加包名、

5.得到key、

二:下载定位SDK,下载下来有地图SDK和定位SDK,然后导入项目,导入再Add As Library,so文件按自己需求来

----------下载地址:/api/android-location-sdk/download/

三:在AndroidManifest.xml的application下配置key和注册service

<!--高德appkey--><meta-dataandroid:name="com.amap.api.v2.apikey"android:value="你的key" /><!--高德service--><service android:name="com.amap.api.location.APSService" />

四:添加权限

<!--高德权限--><!--地图包、搜索包需要的基础权限--><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!--定位包、导航包需要的额外权限(注:基础权限也需要)--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /><uses-permission android:name="android.permission.WRITE_SETTINGS" />

五:地图xml布局实现

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:id="@+id/layout_bottom"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:background="@color/textview_qianhui"android:orientation="vertical"android:padding="5dp"android:visibility="gone"><TextViewandroid:id="@+id/map_shop_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:text="西贝莜面村(丰台店)"android:textSize="16sp" /><TextViewandroid:id="@+id/map_shop_address"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:text="丰葆路,永旺梦乐城四楼" /><LinearLayoutandroid:id="@+id/map_shop_goto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:id="@+id/map_my_address11"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/map_button_color"android:gravity="center"android:padding="5dp"android:text="我的位置"android:textSize="16sp" /><TextViewandroid:id="@+id/map_goto_address11"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:background="@color/map_button_color"android:gravity="center"android:padding="5dp"android:text="开启导航"android:textSize="16sp" /></LinearLayout></LinearLayout><LinearLayoutandroid:id="@+id/layout_bottom_new"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:background="@color/map_button_color"android:orientation="vertical"android:padding="5dp"android:visibility="gone"><TextViewandroid:id="@+id/tv_getLat"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:padding="5dp"android:text="当前经度:"android:textSize="16sp" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@color/textview_hui" /><TextViewandroid:id="@+id/tv_getLon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:padding="5dp"android:text="当前纬度:"android:textSize="16sp" /></LinearLayout><com.amap.api.maps.MapViewandroid:id="@+id/map"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@id/layout_bottom_new"android:clickable="true"android:enabled="true" /><ImageViewandroid:id="@+id/img_back"android:layout_width="30dp"android:layout_height="30dp"android:layout_marginLeft="10dp"android:layout_marginTop="20dp"android:background="@mipmap/map_back" /><ImageViewandroid:id="@+id/img_save"android:layout_width="30dp"android:layout_height="30dp"android:layout_alignParentRight="true"android:layout_marginRight="10dp"android:layout_marginTop="20dp"android:background="@mipmap/save"android:visibility="gone" /><ImageViewandroid:id="@+id/map_my_address"android:layout_width="30dp"android:layout_height="30dp"android:layout_alignBottom="@id/map"android:layout_marginBottom="60dp"android:layout_marginLeft="10dp"android:background="@mipmap/my_address" /></RelativeLayout>

六:地图activity实现

package com.zjtd.bzcommunity.map;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.ImageView;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.maps.AMap;import com.amap.api.maps.CameraUpdateFactory;import com.amap.api.maps.MapView;import com.amap.api.maps.model.BitmapDescriptor;import com.amap.api.maps.model.BitmapDescriptorFactory;import com.amap.api.maps.model.LatLng;import com.amap.api.maps.model.MarkerOptions;import com.zjtd.bzcommunity.R;import java.text.SimpleDateFormat;import java.util.Date;/*** Created by Administrator on /3/23.* 高德地图*/public class ShowMapActivity extends AppCompatActivity implements View.OnClickListener {private MapView mapView;//地图控件private ImageView img_back;//返回键private ImageView map_my_address;//复位private AMap aMap;//地图控制器对象//声明AMapLocationClient类对象public AMapLocationClient mLocationClient = null;//声明mLocationOption对象public AMapLocationClientOption mLocationOption = null;private double lat;private double lon;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.map);initlayout();//初始化定位mLocationClient = new AMapLocationClient(getApplicationContext());//设置定位回调监听mLocationClient.setLocationListener(mLocationListener);//必须要写mapView.onCreate(savedInstanceState);init();}/*** 实例化*/private void initlayout() {mapView = (MapView) findViewById(R.id.map);img_back = (ImageView) findViewById(R.id.img_back);map_my_address = (ImageView) findViewById(R.id.map_my_address);img_back.setOnClickListener(this);map_my_address.setOnClickListener(this);}/*** * 初始化AMap对象*/private void init() {if (aMap == null) {aMap = mapView.getMap();}setUpMap();}/*** 配置定位参数*/private void setUpMap() {//初始化定位参数mLocationOption = new AMapLocationClientOption();//设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//设置是否返回地址信息(默认返回地址信息)mLocationOption.setNeedAddress(true);//设置是否只定位一次,默认为falsemLocationOption.setOnceLocation(false);//设置是否强制刷新WIFI,默认为强制刷新mLocationOption.setWifiActiveScan(true);//设置是否允许模拟位置,默认为false,不允许模拟位置mLocationOption.setMockEnable(false);//设置定位间隔,单位毫秒,默认为2000msmLocationOption.setInterval(2000);//给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);//启动定位mLocationClient.startLocation();}/*** 声明定位回调监听器*/public AMapLocationListener mLocationListener = new AMapLocationListener() {@Overridepublic void onLocationChanged(AMapLocation amapLocation) {if (amapLocation != null) {if (amapLocation.getErrorCode() == 0) {//定位成功回调信息,设置相关消息amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表amapLocation.getLatitude();//获取纬度amapLocation.getLongitude();//获取经度amapLocation.getAccuracy();//获取精度信息SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(amapLocation.getTime());df.format(date);//定位时间amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。amapLocation.getCountry();//国家信息amapLocation.getProvince();//省信息amapLocation.getCity();//城市信息amapLocation.getDistrict();//城区信息amapLocation.getStreet();//街道信息amapLocation.getStreetNum();//街道门牌号信息amapLocation.getCityCode();//城市编码amapLocation.getAdCode();//地区编码amapLocation.getAoiName();//获取当前定位点的AOI信息lat = amapLocation.getLatitude();lon = amapLocation.getLongitude();Log.v("pcw", "lat : " + lat + " lon : " + lon);Log.v("pcw", "Country : " + amapLocation.getCountry() + " province : " + amapLocation.getProvince() + " City : " + amapLocation.getCity() + " District : " + amapLocation.getDistrict());//清空缓存位置aMap.clear();// 设置当前地图显示为当前位置aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));MarkerOptions markerOptions = new MarkerOptions();markerOptions.position(new LatLng(lat, lon));markerOptions.title("当前位置");markerOptions.visible(true);BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.btn_kuaisong_change_icon));markerOptions.icon(bitmapDescriptor);aMap.addMarker(markerOptions);} else {//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。Log.e("AmapError", "location Error, ErrCode:"+ amapLocation.getErrorCode() + ", errInfo:"+ amapLocation.getErrorInfo());}}}};/*** 重新绘制加载地图*/@Overrideprotected void onResume() {super.onResume();mapView.onResume();}/*** 暂停地图的绘制*/@Overrideprotected void onPause() {super.onPause();mapView.onPause();}/*** 保存地图当前的状态方法必须重写*/@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mapView.onSaveInstanceState(outState);}/*** 销毁地图*/@Overrideprotected void onDestroy() {super.onDestroy();mapView.onDestroy();}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.img_back:finish();break;}}}

效果图:

七:定位功能实现、

1.效果图。

2.activity的实现

public class LoginAndRegisterActivity extends Activity implements OnClickListener {/*** 登录界面*/private EditText et_login_phoneNumber;//手机号private EditText et_login_password;//密码private RelativeLayout btn_login;//登录private TextView tv_forget;//忘记密码private TextView textzc;//注册private ImageView imagqk;//清空密码private String address;//地址private LinearLayout lineargg;//随便逛逛private ImageView fanhuicc;//返回键Register//声明AMapLocationClient类对象public AMapLocationClient mLocationClient = null;//声明定位回调监听器//可以通过类implement方式实现AMapLocationListener接口,也可以通过创造接口类对象的方法实现public AMapLocationListener mLocationListener = new AMapLocationListener() {@Overridepublic void onLocationChanged(AMapLocation amapLocation) {if (amapLocation != null) {if (amapLocation.getErrorCode() == 0) {//可在其中解析amapLocation获取相应内容。amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表amapLocation.getLatitude();//获取纬度amapLocation.getLongitude();//获取经度amapLocation.getAccuracy();//获取精度信息amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。amapLocation.getCountry();//国家信息amapLocation.getProvince();//省信息amapLocation.getCity();//城市信息amapLocation.getDistrict();//城区信息amapLocation.getStreet();//街道信息amapLocation.getStreetNum();//街道门牌号信息amapLocation.getCityCode();//城市编码amapLocation.getAdCode();//地区编码amapLocation.getAoiName();//获取当前定位点的AOI信息amapLocation.getBuildingId();//获取当前室内定位的建筑物IdamapLocation.getFloor();//获取当前室内定位的楼层//amapLocation.getGpsStatus();//获取GPS的当前状态//获取定位时间SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(amapLocation.getTime());df.format(date);address = amapLocation.getAddress();} else {//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。Log.e("AmapError", "location Error, ErrCode:"+ amapLocation.getErrorCode() + ", errInfo:"+ amapLocation.getErrorInfo());}}}};//声明AMapLocationClientOption对象public AMapLocationClientOption mLocationOption = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_login);initViews();init();}/*** 实例化*/private void initViews() {et_login_phoneNumber = (EditText) findViewById(R.id.et_login_phoneNumber);et_login_password = (EditText) findViewById(R.id.et_login_password);btn_login = (RelativeLayout) findViewById(R.id.btn_login);tv_forget = (TextView) findViewById(R.id.tv_forget);textzc = (TextView) findViewById(R.id.textzc);imagqk = (ImageView) findViewById(R.id.imagqk);lineargg = (LinearLayout) findViewById(R.id.lineargg);fanhuicc = (ImageView) findViewById(R.id.fanhuicc);btn_login.setOnClickListener(this);tv_forget.setOnClickListener(this);textzc.setOnClickListener(this);imagqk.setOnClickListener(this);lineargg.setOnClickListener(this);fanhuicc.setOnClickListener(this);}private void init() {//初始化定位mLocationClient = new AMapLocationClient(getApplicationContext());//设置定位回调监听mLocationClient.setLocationListener(mLocationListener);//初始化AMapLocationClientOption对象mLocationOption = new AMapLocationClientOption();//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//设置是否返回地址信息(默认返回地址信息)mLocationOption.setNeedAddress(true);//设置是否只定位一次,默认为falsemLocationOption.setOnceLocation(false);//设置是否强制刷新WIFI,默认为强制刷新mLocationOption.setWifiActiveScan(true);//设置是否允许模拟位置,默认为false,不允许模拟位置mLocationOption.setMockEnable(false);//设置定位间隔,单位毫秒,默认为2000msmLocationOption.setInterval(2000);//给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);//单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。mLocationOption.setHttpTimeOut(20000);//关闭缓存机制mLocationOption.setLocationCacheEnable(false);//启动定位mLocationClient.startLocation();}

@Overrideprotected void onStop() {super.onStop();mLocationClient.stopLocation();//停止定位 }

/*** 方法必须重写 */@Overrideprotected void onDestroy() {super.onDestroy();mLocationClient.onDestroy();//销毁定位客户端。 }

---------------------------到此定位和地图显示功能已实现,大神勿喷,有用的可以借鉴一下。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。