当前位置: 移动技术网 > IT编程>移动开发>Android > Android之高德地图定位SDK集成及地图功能实现

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

貌似纯洁txt全集下载,额勒金德,艾滋病 试纸

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

网址:

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

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

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

4.添加包名

5.得到key

二:下载定位sdk,下载下来有地图sdk和定位sdk,然后导入项目,导入再add as library,so文件按自己需求来

下载地址:

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

<!--高德appkey-->
<meta-data
 android: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="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <linearlayout
  android: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">

  <textview
   android:id="@+id/map_shop_name"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="西贝莜面村(丰台店)"
   android:textsize="16sp" />

  <textview
   android:id="@+id/map_shop_address"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="丰葆路,永旺梦乐城四楼" />

  <linearlayout
   android: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">

   <textview
    android: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" />

   <textview
    android: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>

 <linearlayout
  android: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">

  <textview
   android: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" />

  <view
   android:layout_width="match_parent"
   android:layout_height="1dp"
   android:background="@color/textview_hui" />

  <textview
   android: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.mapview
  android: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" />

 <imageview
  android: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" />

 <imageview
  android: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" />

 <imageview
  android: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 2017/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;

 @override
 protected 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);
  //设置是否只定位一次,默认为false
  mlocationoption.setoncelocation(false);
  //设置是否强制刷新wifi,默认为强制刷新
  mlocationoption.setwifiactivescan(true);
  //设置是否允许模拟位置,默认为false,不允许模拟位置
  mlocationoption.setmockenable(false);
  //设置定位间隔,单位毫秒,默认为2000ms
  mlocationoption.setinterval(2000);
  //给定位客户端对象设置定位参数
  mlocationclient.setlocationoption(mlocationoption);
  //启动定位
  mlocationclient.startlocation();
 }
 /**
  * 声明定位回调监听器
  */
 public amaplocationlistener mlocationlistener = new amaplocationlistener() {
  @override
  public 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());
    }
   }
  }
 };

 /**
  * 重新绘制加载地图
  */
 @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();
 }

 @override
 public 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() {
  @override
  public 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();//获取当前室内定位的建筑物id
     amaplocation.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;

 @override
 protected 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);
  //设置是否只定位一次,默认为false
  mlocationoption.setoncelocation(false);
  //设置是否强制刷新wifi,默认为强制刷新
  mlocationoption.setwifiactivescan(true);
  //设置是否允许模拟位置,默认为false,不允许模拟位置
  mlocationoption.setmockenable(false);
  //设置定位间隔,单位毫秒,默认为2000ms
  mlocationoption.setinterval(2000);
  //给定位客户端对象设置定位参数
  mlocationclient.setlocationoption(mlocationoption);
  //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
  mlocationoption.sethttptimeout(20000);
  //关闭缓存机制
  mlocationoption.setlocationcacheenable(false);
  //启动定位
  mlocationclient.startlocation();
 }
@override
protected void onstop() {
 super.onstop();
 mlocationclient.stoplocation();//停止定位 
}
/**
 * 方法必须重写 
 */
@override
protected void ondestroy() {
 super.ondestroy();
 mlocationclient.ondestroy();//销毁定位客户端。 
}

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网