安卓高德地图开发使用
来源:互联网 发布:安卓修改mac地址软件 编辑:程序博客网 时间:2024/04/30 15:31
首先是去官网下载相应的地图sdk
在项目中导入相应的jar包
配置 AndroidManifest.xml
声明权限,设置高德Key,在application标签中加入key
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xw.bordercar.ext.amap"> <!--允许程序打开网络套接字--> <uses-permission android:name="android.permission.INTERNET" /> <!--允许程序设置内置sd卡的写权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--允许程序获取网络状态--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--允许程序访问WiFi网络信息--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!--允许程序读写手机状态和身份--> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!--允许程序访问CellID或WiFi热点来获取粗略的位置--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <application android:allowBackup="true" android:supportsRtl="true"> <activity android:name=".TestActivity"/> <meta-data android:name="com.amap.api.v2.apikey" android:value="自己申请的key" /> </application></manifest>
显示地图
在布局xml文件中添加地图控件:
<com.amap.api.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="200dp" />
在项目中使用地图的时候需要注意,由于SDK并没有提供用于管理地图生命周期的Activity,因此需要用户继承Activity后管理地图的生命周期,防止内存泄露,示例代码如下:
public class MainActivity extends Activity { MapView mMapView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取地图控件引用 mMapView = (MapView) findViewById(R.id.map); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理 mMapView.onCreate(savedInstanceState); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理 mMapView.onSaveInstanceState(outState); }}
以上就可以显示高德地图了。
下面这个看情况是否需要,有时候我们的布局在mapview外面会有一层ScrollView,如果我们是想滑动地图的话,会发现是滑动了ScrollView了,为了解决这个问题,加入如下:
// 重写onTouch()事件,在事件里通过requestDisallowInterceptTouchEvent(boolean)方法来设置父类的不可用,true表示父类的不可用 // 解决地图的touch事件和scrollView的touch事件冲突问题 aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() { @Override public void onTouch(MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { sv.requestDisallowInterceptTouchEvent(false); } else { sv.requestDisallowInterceptTouchEvent(true); } } });
地图定位
初始化定位
请在主线程中声明AMapLocationClient类对象,需要传Context类型的参数。推荐用getApplicationConext()方法获取全进程有效的context。
//声明AMapLocationClient类对象public AMapLocationClient mLocationClient = null;//声明定位回调监听器public AMapLocationListener mLocationListener = new AMapLocationListener();//初始化定位mLocationClient = new AMapLocationClient(getApplicationContext());//设置定位回调监听mLocationClient.setLocationListener(mLocationListener);
配置参数并启动定位
private void configLocationOption() { //初始化定位参数 AMapLocationClientOption mLocationOption = new AMapLocationClientOption(); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否只定位一次,默认为false mLocationOption.setOnceLocation(false);// mLocationOption.setOnceLocationLatest(true); //设置是否强制刷新WIFI,默认为强制刷新 mLocationOption.setWifiActiveScan(true); //设置是否允许模拟位置,默认为false,不允许模拟位置 mLocationOption.setMockEnable(false); //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。 mLocationOption.setInterval(50000); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption);}
//获取一次定位结果://该方法默认为false。mLocationOption.setOnceLocation(true);//获取最近3s内精度最高的一次定位结果://设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。mLocationOption.setOnceLocationLatest(true);}
启动定位
//给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);//启动定位mLocationClient.startLocation();
实现监听器
//可以通过类implement方式实现AMapLocationListener接口,也可以通过创造接口类对象的方法实现//以下为后者的举例:// 定位回调监听器AMapLocationListener mAMapLocationListener = new AMapLocationListener(){@Overridepublic void onLocationChanged(AMapLocation amapLocation) { }}
解析AMapLocation对象
//首先,可以判断AMapLocation对象不为空,当定位错误码类型为0时定位成功if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) {//可在其中解析amapLocation获取相应内容。 }else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError","location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); }}
当定位成功时,可在如上判断中解析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);
停止定位
@Overrideprotected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); if (mLocationClient != null) { mLocationClient.stopLocation();//停止定位 mLocationClient.onDestroy();//销毁定位客户端。 }}
以下是地图的一些常用功能:
在定位回调监听器里:定位成功后,地图移到定位点,并获取地址
if (aMapLocation != null && aMapLocation.getErrorCode() == ERROR_CODE.ERROR_NONE) { aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()), 15)); String addressStr = aMapLocation.getCity() + aMapLocation.getDistrict() + aMapLocation.getStreet(); if (!TextUtils.isEmpty(aMapLocation.getAoiName())) { addressStr += aMapLocation.getAoiName(); } else { addressStr = addressStr + aMapLocation.getPoiName() + "附近"; }}
移动地图到某个坐标点
LatLonPoint latLonPoint = new LatLonPoint(latitude, longitude);aMap.animateCamera(CameraUpdateFactory.newLatLngZoom( AMapUtil.convertToLatLng(latLonPoint), 15));
如图下所示这种可用一下布局:
<FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3"> <com.amap.api.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/amap_icon_select_location" android:translationY="-10dp" /></FrameLayout>
地图选址
aMap.setOnCameraChangeListener(this); // 添加移动地图事件监听器@Overridepublic void onCameraChange(CameraPosition cameraPosition) {}@Overridepublic void onCameraChangeFinish(CameraPosition cameraPosition) { LatLngEntity latLngEntity = new LatLngEntity(cameraPosition.target.latitude, cameraPosition.target.longitude); GeoCoderUtil.getInstance(SelectLocationActivity.this).geoAddress(latLngEntity, new GeoCoderUtil.GeoCoderAddressListener() { @Override public void onAddressResult(String result) { locationAddress = result; addressDesc.setText(result); } });}
package com.xw.bordercar.ext.amap.location;import android.content.Context;import android.text.TextUtils;import com.amap.api.services.core.LatLonPoint;import com.amap.api.services.geocoder.GeocodeAddress;import com.amap.api.services.geocoder.GeocodeQuery;import com.amap.api.services.geocoder.GeocodeResult;import com.amap.api.services.geocoder.GeocodeSearch;import com.amap.api.services.geocoder.RegeocodeAddress;import com.amap.api.services.geocoder.RegeocodeQuery;import com.amap.api.services.geocoder.RegeocodeResult;import com.xw.bordercar.ext.amap.model.LatLngEntity;public class GeoCoderUtil implements GeocodeSearch.OnGeocodeSearchListener {private GeocodeSearch geocodeSearch;private GeoCoderAddressListener geoCoderAddressListener;private GeoCoderLatLngListener geoCoderLatLngListener;private static GeoCoderUtil geoCoderUtil;public static GeoCoderUtil getInstance(Context context) { if (null == geoCoderUtil) { geoCoderUtil = new GeoCoderUtil(context); } return geoCoderUtil;}private GeoCoderUtil(Context context) { geocodeSearch = new GeocodeSearch(context); geocodeSearch.setOnGeocodeSearchListener(this);}/** * 经纬度转地址描述 **/public void geoAddress(String latLng, GeoCoderAddressListener geoCoderAddressListener) { if (TextUtils.isEmpty(latLng)) { geoCoderAddressListener.onAddressResult(""); return; } this.geoAddress(new LatLngEntity(latLng), geoCoderAddressListener);}/** * 经纬度转地址描述 **/public void geoAddress(LatLngEntity latLngEntity, GeoCoderAddressListener geoCoderAddressListener) { if (latLngEntity == null) { geoCoderAddressListener.onAddressResult(""); return; } this.geoCoderAddressListener = geoCoderAddressListener; LatLonPoint latLonPoint = new LatLonPoint(latLngEntity.getLatitude(), latLngEntity.getLongitude()); // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP); geocodeSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求}/** * 地址描述转经纬度 **/public void geoLatLng(String cityName, String address, GeoCoderLatLngListener geoCoderLatLngListener) { if (TextUtils.isEmpty(cityName) || TextUtils.isEmpty(address)) { geoCoderLatLngListener.onLatLngResult(null); return; } this.geoCoderLatLngListener = geoCoderLatLngListener; GeocodeQuery query = new GeocodeQuery(address, cityName); geocodeSearch.getFromLocationNameAsyn(query);}@Overridepublic void onRegeocodeSearched(RegeocodeResult result, int rCode) { if (rCode != 1000 || result == null || result.getRegeocodeAddress() == null) { geoCoderAddressListener.onAddressResult(""); return; } RegeocodeAddress regeocodeAddress = result.getRegeocodeAddress(); String addressDesc = regeocodeAddress.getCity() + regeocodeAddress.getDistrict() + regeocodeAddress.getTownship() + regeocodeAddress.getStreetNumber().getStreet(); if (regeocodeAddress.getAois().size() > 0) { addressDesc += regeocodeAddress.getAois().get(0).getAoiName(); } geoCoderAddressListener.onAddressResult(addressDesc);}@Overridepublic void onGeocodeSearched(GeocodeResult geocodeResult, int rCode) { if (geocodeResult == null || geocodeResult.getGeocodeAddressList() == null || geocodeResult.getGeocodeAddressList().size() <= 0) { geoCoderLatLngListener.onLatLngResult(null); return; } GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0); geoCoderLatLngListener.onLatLngResult(new LatLngEntity(address.getLatLonPoint()));}public interface GeoCoderAddressListener { void onAddressResult(String result);}public interface GeoCoderLatLngListener { void onLatLngResult(LatLngEntity latLng);}}
添加自定义的market
View overlayView = View.inflate(this, R.layout.overlay_item, null); TextView tvAddress = (TextView) overlayView.findViewById(R.id.tv_address); tvAddress.setText("test"); LatLonPoint latLonPoint = new LatLonPoint(latitude, longitude); BitmapDescriptor bd = BitmapDescriptorFactory.fromView(overlayView); MarkerOptions oo = new MarkerOptions().position(AMapUtil.convertToLatLng(latLonPoint)).icon(bd).zIndex(9).draggable(true); aMap.addMarker(oo);
overlay_item2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textSize="12sp" android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/shape_infowindow" /> <ImageView android:id="@+id/img_locIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="2dp" android:src="@drawable/amap_icon_select_location"/></LinearLayout>
0 0
- 安卓高德地图开发使用
- 使用VirtualEarth开发地图程序
- 使用VirtualEarth开发地图程序
- IOS开发 使用地图 MapKit
- IOS开发 地图定位 使用
- IOS开发 使用地图 MapKit
- IOS开发 使用地图 MapKit
- iOS地图开发-基本使用
- IOS开发 使用地图 MapKit
- IOS开发 使用地图 MapKit
- 安卓高德地图的使用
- 安卓高德地图的使用
- 安卓高德地图的使用
- 使用百度地图接口实现自定义网页地图开发实现
- android开发游记:百度地图SDK基础地图的使用
- Android 百度地图开发教程(一) 地图使用入门及地图表面覆盖物使用
- ArcGIS Server ADF开发:使用影像地图
- GMap.NET地图开发控件使用心得
- Codeforces Round #384 (Div. 2) A Vladik and flights
- 【Android】搭建android开发环境
- 微信企业号之通讯录开发
- 如何调试iOS App中UIWebView和WKWebView使用的HTML文件
- VS2013安装卡死问题及解决方法
- 安卓高德地图开发使用
- React-Native学习指南
- 小米sdk接入
- 阿里云Centos搭建java全攻略(jdk1.8+tomcat9.0+mysql5.7)
- Python __new__() 方法与__init__()方法
- Android开发设计模式之——单例模式【转】
- 机器浮点数的范围
- JavaScript数据类型隐式转换原则总结(干货!)
- Input框 type 类型 password 转换为label 的方法