高德地图之基础地图和定位

来源:互联网 发布:周杰伦的淘宝店是什么 编辑:程序博客网 时间:2024/05/23 16:21

大部分的功能其实在         安卓地图SDK  中可以找到..因为包含了一系列的实例代码,所以很方便,但是为什么没有直接链接写下来结束呢?是因为中间遇到了一些小的问题.----定位不到,当然现在也能实现了(解决方案如下定位部分),下面是我的一些小的建议和转载的一些文章

其实更加重要的是地图开发的一些文档,索要仔细阅读,以及xml的配置.

开发环境的配置请访问:http://lbs.amap.com/api/android-sdk/summary/




一、 要实现高德地图定位呢,首先需要做好以下几步准备: 
1. 在高德开放平台注册帐号 
注册地址:http://lbs.amap.com 
2. 在开发中下载Android平台下的地图SDK和定位SDK文件 
高德开放平台主页
地图SDK
进入相关下载下载自己想要的功能或文件,图只是截取了地图SDK的页面,定位SDK也是一样,按自己想要的文件下载。下载完成后解压得到: 
- 3D地图包解压后得到:3D地图显示包“AMap_3DMap_VX.X.X_时间.jar”和库文件夹(包含armeabi、arm64-v8a等库文件)。 
- 2D地图包解压后得到:2D地图显示包“AMap_2DMap_VX.X.X_时间.jar ” 
- 定位SDK下载并解压得到定位包“AMap_Location_V2.x.x.jar“ 
3. 添加jar包,将jar包放入工程的libs目录下。 
 
对于每个jar文件,右键-选择Add As Library,导入到工程中。或者使用菜单栏 选择 File ->Project Structure->Modules-> Dependencies。点击绿色的加号选择File dependency. 然后选择要添加的jar包即可,此时build.gradle中会自动生成如下信息。 
 
4. 申请API KEY 
进入控制台 
主页
创建自己的应用(创建过程内需要的SHA1已经的博客讲过) 
应用

开发环境已经配置好了,接下来就是敲代码了。

二、 首先我们要做的就是将地图显示出来,通过以下几步操作,即可在应用中使用高德地图SDK:

第一步:添加用户key 在工程的“ AndroidManifest.xml ”文件如下代码中添加您的用户 Key。

<code class="hljs perl has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"> <application        android:allowBackup=<span class="hljs-string" style="color: rgb(0, 136, 0);">"true"</span>        android:icon=<span class="hljs-string" style="color: rgb(0, 136, 0);">"<span class="hljs-variable" style="color: rgb(102, 0, 102);">@mipmap</span>/ic_launcher"</span>        android:label=<span class="hljs-string" style="color: rgb(0, 136, 0);">"<span class="hljs-variable" style="color: rgb(102, 0, 102);">@string</span>/app_name"</span>        android:supportsRtl=<span class="hljs-string" style="color: rgb(0, 136, 0);">"true"</span>        android:theme=<span class="hljs-string" style="color: rgb(0, 136, 0);">"<span class="hljs-variable" style="color: rgb(102, 0, 102);">@style</span>/AppTheme"</span>>        <meta-data            android:name=<span class="hljs-string" style="color: rgb(0, 136, 0);">"com.amap.api.v2.apikey"</span>            android:value=<span class="hljs-string" style="color: rgb(0, 136, 0);">"c9df032baec3ec50b1e089768ea4672b"</span> /></code>

第二步:添加所需权限 在工程的“ AndroidManifest.xml ”文件中进行添加。

<code class="hljs lasso has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//地图包、搜索包需要的基础权限</span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.INTERNET"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.WRITE_EXTERNAL_STORAGE"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_NETWORK_STATE"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_WIFI_STATE"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.READ_PHONE_STATE"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_COARSE_LOCATION"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//定位包、导航包需要的额外权限(注:基础权限也需要)</span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_FINE_LOCATION"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_MOCK_LOCATION"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span>    <span class="hljs-subst"><</span>uses<span class="hljs-attribute">-permission</span> android:name<span class="hljs-subst">=</span><span class="hljs-string" style="color: rgb(0, 136, 0);">"android.permission.CHANGE_WIFI_STATE"</span> <span class="hljs-subst">/</span><span class="hljs-subst">></span></code>

第三步:在布局xml文件中添加地图控件。

<code class="hljs avrasm has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136);">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.amap</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.api</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.maps</span>2d<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.MapView</span>        android:id=<span class="hljs-string" style="color: rgb(0, 136, 0);">"@+id/map_view"</span>        android:layout_width=<span class="hljs-string" style="color: rgb(0, 136, 0);">"match_parent"</span>        android:layout_height=<span class="hljs-string" style="color: rgb(0, 136, 0);">"match_parent"</span> /></code>

第四步,创建地图Activity,管理地图生命周期。

<code class="hljs java has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">Activity</span> {</span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> MapView mMapView = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">null</span>;  <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//获取地图控件引用</span>    mMapView = (MapView) findViewById(R.id.map);    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理</span>    mMapView.onCreate(savedInstanceState);  }  <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onDestroy</span>() {    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onDestroy();    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理</span>    mMapView.onDestroy();  } <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onResume</span>() {    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onResume();    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理</span>    mMapView.onResume();    } <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onPause</span>() {    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onPause();    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理</span>    mMapView.onPause();    } <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onSaveInstanceState</span>(Bundle outState) {    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onSaveInstanceState(outState);    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理</span>    mMapView.onSaveInstanceState(outState);  } }</code>

注意:一定要有mMapView.onCreate(savedInstanceState); 
运行一下,效果如下: 

三、接下来就是实现定位了,定位也需要通过以下几步操作完成:

第一步:配置AndroidManifest.xml ------该步骤也是我定位的时候没有添加的..

1.在application标签中声明service组件,每个app拥有自己单独的定位service。

<code class="hljs xml has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="color:#ff0000;"> <span class="hljs-tag"><<span class="hljs-title">service</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.amap.api.location.APSService"</span>/></span></span></code>

2.接下来声明使用权限

<code class="hljs xml has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--用于进行网络定位--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_COARSE_LOCATION"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--用于访问GPS定位--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_FINE_LOCATION"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--获取运营商信息,用于支持提供运营商信息相关的接口--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_NETWORK_STATE"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--用于访问wifi网络信息,wifi信息会用于进行网络定位--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.ACCESS_WIFI_STATE"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.CHANGE_WIFI_STATE"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--用于访问网络,网络定位需要上网--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.INTERNET"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--用于读取手机当前的状态--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.READ_PHONE_STATE"</span> /></span>    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据--></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /></span></code>

第二步:启动定位功能:

1. 在主线程中获得地图对象AMap,并设置定位监听且实现LocationSource接口:
<code class="hljs java has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">Activity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">implements</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">LocationSource</span>{</span></code>
<code class="hljs cs has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (aMap == <span class="hljs-keyword" style="color: rgb(0, 0, 136);">null</span>) {            aMap = mapView.getMap();            <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置显示定位按钮 并且可以点击</span>            UiSettings settings = aMap.getUiSettings();            aMap.setLocationSource(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置了定位的监听,这里要实现LocationSource接口</span>            <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 是否显示定位按钮</span>            settings.setMyLocationButtonEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>);            aMap.setMyLocationEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//显示定位层并且可以触发定位,默认是flase</span>        }</code>
2. 配置定位参数,启动定位
<code class="hljs cs has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//初始化定位</span>        mLocationClient = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> AMapLocationClient(getApplicationContext());        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置定位回调监听,这里要实现AMapLocationListener接口,AMapLocationListener接口只有onLocationChanged方法可以实现,用于接收异步返回的定位结果,参数是AMapLocation类型。</span>        mLocationClient.setLocationListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//初始化定位参数</span>        mLocationOption = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> AMapLocationClientOption();        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式</span>        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置是否返回地址信息(默认返回地址信息)</span>        mLocationOption.setNeedAddress(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置是否只定位一次,默认为false</span>        mLocationOption.setOnceLocation(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">false</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置是否强制刷新WIFI,默认为强制刷新</span>        mLocationOption.setWifiActiveScan(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置是否允许模拟位置,默认为false,不允许模拟位置</span>        mLocationOption.setMockEnable(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">false</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//设置定位间隔,单位毫秒,默认为2000ms</span>        mLocationOption.setInterval(<span class="hljs-number" style="color: rgb(0, 102, 102);">2000</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//给定位客户端对象设置定位参数</span>        mLocationClient.setLocationOption(mLocationOption);        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//启动定位</span>        mLocationClient.startLocation()</code>
  • 高精度定位模式:

    在这种定位模式下,将同时使用高德网络定位和GPS定位,优先返回精度高的定位
  • 低功耗定位模式:

    在这种模式下,将只使用高德网络定位
  • 仅设备定位模式:

    在这种模式下,将只使用GPS定位。 3. 实现AMapLocationListener接口,获取定位结果:
<code class="hljs java has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">Activity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">implements</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">LocationSource</span>, <span class="hljs-title" style="color: rgb(102, 0, 102);">AMapLocationListener</span> {</span></code><ul class="pre-numbering"><li>1</li></ul>
<code class="hljs avrasm has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">@Overridepublic void onLocationChanged(AMapLocation aMapLocation) {    if (aMapLocation != null) {        if (aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getErrorCode</span>() == <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {            //定位成功回调信息,设置相关消息            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getLocationType</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//获取当前定位结果来源,如网络定位结果,详见官方定位类型表</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getLatitude</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//获取纬度</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getLongitude</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//获取经度</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getAccuracy</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//获取精度信息</span>            SimpleDateFormat df = new SimpleDateFormat(<span class="hljs-string" style="color: rgb(0, 136, 0);">"yyyy-MM-dd HH:mm:ss"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>            Date date = new Date(aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getTime</span>())<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>            df<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.format</span>(date)<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//定位时间</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getAddress</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getCountry</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//国家信息</span>            <span style="color:#ff0000;">aMapLocation<span class="hljs-preprocessor">.getProvince</span>()<span class="hljs-comment">;//省信息</span>            aMapLocation<span class="hljs-preprocessor">.getCity</span>()<span class="hljs-comment">;//城市信息</span></span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getDistrict</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//城区信息</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getStreet</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//街道信息</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getStreetNum</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//街道门牌号信息</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getCityCode</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//城市编码</span>            aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getAdCode</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;//地区编码</span>            // 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置            if (isFirstLoc) {                //设置缩放级别                aMap<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.moveCamera</span>(CameraUpdateFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.zoomTo</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">17</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>                //将地图移动到定位点                aMap<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.moveCamera</span>(CameraUpdateFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.changeLatLng</span>(new LatLng(aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getLatitude</span>(), aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getLongitude</span>())))<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>                //点击定位按钮 能够将地图的中心移动到定位点                mListener<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.onLocationChanged</span>(aMapLocation)<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>                //获取定位信息                StringBuffer buffer = new StringBuffer()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>                buffer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.append</span>(aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getCountry</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>                        + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getProvince</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>                        + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getCity</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>                        + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getProvince</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>                        + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getDistrict</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>                        + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getStreet</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>                        + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getStreetNum</span>())<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>                Toast<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.makeText</span>(getApplicationContext(), buffer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.toString</span>(), Toast<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.LENGTH</span>_LONG)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.show</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>                isFirstLoc = false<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>            }        } else {            //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。            Log<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.e</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"AmapError"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"location Error, ErrCode:"</span>                    + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getErrorCode</span>() + <span class="hljs-string" style="color: rgb(0, 136, 0);">", errInfo:"</span>                    + aMapLocation<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.getErrorInfo</span>())<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>            Toast<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.makeText</span>(getApplicationContext(), <span class="hljs-string" style="color: rgb(0, 136, 0);">"定位失败"</span>, Toast<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.LENGTH</span>_LONG)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">.show</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>        }    }}</code>
4.关于停止定位
<code class="hljs java has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onDestroy</span>() {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onDestroy();        mapView.onDestroy();        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//mLocationClient.stopLocation();//停止定位</span>        mLocationClient.onDestroy();<span class="hljs-comment" style="color: rgb(136, 0, 0);">//销毁定位客户端。</span>        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//销毁定位客户端之后,若要重新开启定位请重新New一个AMapLocationClient对象。</span>    }<span class="hljs-comment" style="color: rgb(136, 0, 0);">//激活定位</span>    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">activate</span>(OnLocationChangedListener onLocationChangedListener) {        mListener = onLocationChangedListener;    }    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">deactivate</span>() {        mListener = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">null</span>;    }</code><code class="hljs java has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></code><code class="hljs java has-numbering" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">大部分是转载的志哥的.....希望对你有所帮助</code>
1 0
原创粉丝点击