软件编程
位置:首页>> 软件编程>> Android编程>> Android之高德地图定位SDK集成及地图功能实现

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

作者:瞳瞳色丶轻烟  发布时间:2023-12-06 07:33:16 

标签:android,高德地图

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

网址:http://lbs.amap.com/

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

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

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

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

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

4.添加包名

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

5.得到key

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

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

下载地址:http://lbs.amap.com/api/android-location-sdk/download/

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

三:在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;
 }
}
}

效果图:

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

七:定位功能实现

1.效果图

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

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
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com