Android之高德地图定位SDK集成及地图功能实现
作者:瞳瞳色丶轻烟 发布时间:2023-12-06 07:33:16
标签:android,高德地图
一:百度高德官方网站,然后去创建应用
网址:http://lbs.amap.com/
1.找到控制台创建一个应用
2.添加key名称,注意命名规范,还有就是下面的SHA1和包名
3.点击右边的Gradle再选择signingReport下面会有个命令,稍等几分钟得到SHA1
4.添加包名
5.得到key
二:下载定位SDK,下载下来有地图SDK和定位SDK,然后导入项目,导入再Add As Library,so文件按自己需求来
下载地址:http://lbs.amap.com/api/android-location-sdk/download/
三:在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();//销毁定位客户端。
}
到此定位和地图显示功能已实现,大神勿喷,有用的可以借鉴一下。
来源:http://blog.csdn.net/Android_Cll/article/details/65447137
0
投稿
猜你喜欢
- 上篇文章给大家介绍了springboot全局字符编码设置解决乱码问题 感兴趣的朋友可以点击查看,下面通过两种方式给大家介绍Spri
- 今天因为发布swagger-spring-boot-starter做一个问题的修复,然后碰到了下面这个问题,记录一下解决过程,帮助后续碰到类
- C#的MVC寻找对应的控制器首先是寻找当前域的如果找不到就会寻找备用的..但是有些时候我们是不想他去寻找备用的控制器.这里就涉及到了一个Da
- Android中实现进度条有很多种方式,自定义进度条一般是继承progressBar或继承view来实现,本篇中讲解的是第二种方式。先上效果
- 1.非静态成员变量当成员变量为非静态成员变量且对当前类进行实例化时,将会产生死循环例子:public class ConstructorCl
- 1.Fragment概述在一个Activity中, Fragment代表UI的一个部分或者一个行为。一个Activity可以结合多个Frag
- 1、来源random.nextInt() 为 java.util.Random类中的方法; Math.random() 为 java.lan
- java 多线程的几种实现方法总结1.多线程有几种实现方法?同步有几种实现方法?多线程有两种实现方法,分别是继承Thread类与实现Runn
- App Crash对于用户来讲是一种最糟糕的体验,它会导致流程中断、app口碑变差、app卸载、用户流失、订单流失等。相关数据显示,当And
- 本文实例讲述了C#实现XML与实体类之间相互转换的方法。分享给大家供大家参考,具体如下:using System;using System.
- 干java 开发这么多年, 之前一直没留意java 进程还区分守护进程和用户进程。守护进程这个概念最早还是在linux系统中接触的,直到近期
- 一 Flow使用注意事项多个Flow不能放到一个lifecycleScope.launch里去collect{},因为进入collect{}
- 使用Aspose.Cells创建和读取Excel文件,供大家参考,具体内容如下1. 创建ExcelAspose.Cells.License
- 1、概述 限流的含义是在单位时间内确保发往某个模块的请求数量小于某个数值,比如在实现秒杀功能时,需要确保在10秒内发往支付模块的请求数量小
- Java中,for-each循环简化了任何Collection或array的遍历过程,但并不是每个Java程序员都了解本文将要描述的for-
- 在使用java项目时,如果没有详细的管理和辅助流程,就会像程序失去了系统的调配一样。在java中有一种专门管理项目的工具,叫做maven,除
- 刚刚开始学习C#,想自己做一个网页游戏的挂。游戏里面有收钱的动作,一个建筑物一个建筑物的点,很累啊。于是想用C#模拟鼠标操作替我收钱,想着学
- Condition是在Spring 4.0 增加的条件判断功能,通过这个可以功能可以实现选择性的创建 Bean操作。思考:SpringBoo
- 本文实例讲述了Java操作redis实现增删查改功能的方法。分享给大家供大家参考,具体如下:首先,我们需要在windows下配置一个redi
- 我们知道多线程因为同时处理子线程的能力,对于程序运行来说,能够达到很高的效率。不过很多人对于多线程的执行方法还没有尝试过,本篇我们将为大家介