iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android之高德地图定位SDK集成及地图功能实现
  • 932
分享到

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

高德地图地图高德sdkAndroid 2022-06-06 01:06:24 932人浏览 八月长安
摘要

一:百度高德官方网站,然后去创建应用 网址:Http://lbs.amap.com/ 1.找到控制台创建一个应用 2.添加key名称,注意命名规范,还有就是下面的SHA1和包

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

网址: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;

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);
 }
 
 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();//销毁定位客户端。 
}

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

您可能感兴趣的文章:Android 仿高德地图可拉伸的BottomSheet的示例代码Android基于高德地图完全自定义Marker的实现方法Android调用高德地图定位的方法Android基于高德地图poi的仿微信获取位置功能实例代码Android 高德地图之poi搜索功能的实现代码Android高德地图poi检索仿微信发送位置实例代码Android开发之高德地图实现定位Android中GPS坐标转换为高德地图坐标详解关于Android高德地图的简单开发实例代码(DEMO)Android实现高德地图显示及定位


--结束END--

本文标题: Android之高德地图定位SDK集成及地图功能实现

本文链接: https://www.lsjlt.com/news/21791.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作