高德地图API定位小蓝点实现

来源:互联网 发布:cf进入出现网络异常 编辑:程序博客网 时间:2024/06/05 00:59

高德地图目前的定位类型有 3 种:

LOCATION_TYPE_LOCATE :只在第一次定位移动到地图中心点;

LOCATION_TYPE_MAP_FOLLOW :定位,移动到地图中心点并跟随;

LOCATION_TYPE_MAP_ROTATE :定位,移动到地图中心点,跟踪并根据面向方向旋转地图。


1.首先在 http://id.amap.com/ 站点注册用户名,密码(如果没有):



2. 获得高德地图服务的 APIKey ,高德地图和百度地图的 APIKey 都是相同的,数字证书的 keystore 通常保存在 ANDROID_SDK_HOME 环境变量对应的路径的 .android/ 目录下。获取 APIKey 可有两种方式:

     (1). 在 DOS 命令中输入如下所示(红色部分就是 keystore 的认证):



     (2).在 Eclipse 中的 Window ---------> Preferences ---------> Android -------->Build 如下图所示:



3. 登录 http://lbs.amap.com/console/ 页面获取 KEY ,如下图:


4. 得到 KEY :



5. 登录 http://lbs.amap.com/api/android-sdk/down/ 站点下载所需版本,如下:



6. 登录 http://lbs.amap.com/api/android-location-sdk/down/ 下载定位包,如下图:


7. 将下载的压缩包解压后放入 Eclipse 的 libs 文件下,如下图:


      (1)如果是 AndroidStudio 则把解压得到的 3 个 jar 包放入 libs 目录下,并选中 3个 jar 包,右键单击,选择                        “Add As Library” 将其设为库;

      (2)在 src/main/ 目录下新建一个 jniLibs 子目录,将解压得到的剩余文件夹复制到该目录下。


8.  在 AndroidManifest.xml 中注册相应的权限:


9.  在 <application...../> 元素内添加如下元素,红色部分为获得的 KEY:


10.  添加 service :



11.  activity_main.xml 布局如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.amap.api.maps.MapView  
  8.         android:id="@+id/map"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent" >  
  11.     </com.amap.api.maps.MapView>  
  12.   
  13.     <TextView  
  14.         android:id="@+id/location_errInfo_text"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_gravity="bottom|left"  
  18.         android:layout_marginBottom="10dp"  
  19.         android:layout_marginLeft="10dp"  
  20.         android:background="@color/red"  
  21.         android:textColor="@color/darkgrey"  
  22.         android:text="TextView"   
  23.         android:visibility="gone"/>  
  24.   
  25.     <RadioGroup  
  26.         android:id="@+id/gps_radio_group"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_gravity="top|left"  
  30.         android:layout_marginLeft="10dp"  
  31.         android:layout_marginTop="10dp"  
  32.         android:background="@color/grey"  
  33.         android:orientation="horizontal" >  
  34.   
  35.         <RadioButton  
  36.             android:id="@+id/gps_locate_button"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:checked="true"  
  40.             android:text="@string/gpslocate"  
  41.             android:textColor="@android:color/black" />  
  42.   
  43.         <RadioButton  
  44.             android:id="@+id/gps_follow_button"  
  45.             android:layout_width="wrap_content"  
  46.             android:layout_height="wrap_content"  
  47.             android:text="@string/gpsfollow"  
  48.             android:textColor="@android:color/black" />  
  49.   
  50.         <RadioButton  
  51.             android:id="@+id/gps_rotate_button"  
  52.             android:layout_width="wrap_content"  
  53.             android:layout_height="wrap_content"  
  54.             android:text="@string/gpsrotate"  
  55.             android:textColor="@android:color/black" />  
  56.     </RadioGroup>  
  57.   
  58. </FrameLayout>  
   MainActivity.java :

[java] view plain copy
  1. package com.amap.map3d.demo;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.view.Window;  
  9. import android.widget.RadioGroup;  
  10. import android.widget.RadioGroup.OnCheckedChangeListener;  
  11. import android.widget.TextView;  
  12.   
  13. import com.amap.api.location.AMapLocation;  
  14. import com.amap.api.location.AMapLocationClient;  
  15. import com.amap.api.location.AMapLocationClientOption;  
  16. import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;  
  17. import com.amap.api.location.AMapLocationListener;  
  18. import com.amap.api.maps.AMap;  
  19. import com.amap.api.maps.LocationSource;  
  20. import com.amap.api.maps.MapView;  
  21. import com.amap.map3d.demo.R;  
  22.   
  23. public class MainActivity extends Activity implements LocationSource,  
  24.         AMapLocationListener,OnCheckedChangeListener {  
  25.     private AMap aMap;  
  26.     private MapView mapView;  
  27.     // 处理定位更新  
  28.     private OnLocationChangedListener mListener;  
  29.     // 定位  
  30.     private AMapLocationClient mlocationClient;  
  31.       
  32.     private AMapLocationClientOption mLocationOption;  
  33.     private RadioGroup mGPSModeGroup;  
  34.       
  35.     private TextView mLocationErrText;  
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏  
  41.         setContentView(R.layout.activity_main);  
  42.         mapView = (MapView) findViewById(R.id.map);  
  43.         mapView.onCreate(savedInstanceState);// 此方法必须重写  
  44.         init();  
  45.     }  
  46.   
  47.     /** 
  48.      * 初始化 
  49.      */  
  50.     private void init() {  
  51.         if (aMap == null) {  
  52.             aMap = mapView.getMap();  
  53.             setUpMap();  
  54.         }  
  55.         mGPSModeGroup = (RadioGroup) findViewById(R.id.gps_radio_group);  
  56.         mGPSModeGroup.setOnCheckedChangeListener(this);  
  57.         mLocationErrText = (TextView)findViewById(R.id.location_errInfo_text);  
  58.         mLocationErrText.setVisibility(View.GONE);  
  59.     }  
  60.   
  61.     /** 
  62.      * 设置一些amap的属性 
  63.      */  
  64.     private void setUpMap() {  
  65.         aMap.setLocationSource(this);// 设置定位监听  
  66.         aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示  
  67.         aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false  
  68.         // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种  
  69.         aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);  
  70.     }  
  71.   
  72.     @Override  
  73.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  74.         switch (checkedId) {  
  75.         case R.id.gps_locate_button:  
  76.             // 设置定位的类型为定位模式  
  77.             aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);  
  78.             break;  
  79.         case R.id.gps_follow_button:  
  80.             // 设置定位的类型为 跟随模式  
  81.             aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);  
  82.             break;  
  83.         case R.id.gps_rotate_button:  
  84.             // 设置定位的类型为根据地图面向方向旋转  
  85.             aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);  
  86.             break;  
  87.         }  
  88.   
  89.     }  
  90.   
  91.     /** 
  92.      * 方法必须重写 
  93.      */  
  94.     @Override  
  95.     protected void onResume() {  
  96.         super.onResume();  
  97.         mapView.onResume();  
  98.     }  
  99.   
  100.     /** 
  101.      * 方法必须重写 
  102.      */  
  103.     @Override  
  104.     protected void onPause() {  
  105.         super.onPause();  
  106.         mapView.onPause();  
  107.         deactivate();  
  108.     }  
  109.   
  110.     /** 
  111.      * 方法必须重写 
  112.      */  
  113.     @Override  
  114.     protected void onSaveInstanceState(Bundle outState) {  
  115.         super.onSaveInstanceState(outState);  
  116.         mapView.onSaveInstanceState(outState);  
  117.     }  
  118.   
  119.     /** 
  120.      * 方法必须重写 
  121.      */  
  122.     @Override  
  123.     protected void onDestroy() {  
  124.         super.onDestroy();  
  125.         mapView.onDestroy();  
  126.         if(null != mlocationClient){  
  127.             mlocationClient.onDestroy();  
  128.         }  
  129.     }  
  130.   
  131.     /** 
  132.      * 定位成功后回调函数 
  133.      */  
  134.     @Override  
  135.     public void onLocationChanged(AMapLocation amapLocation) {  
  136.         if (mListener != null && amapLocation != null) {  
  137.             if (amapLocation != null  
  138.                     && amapLocation.getErrorCode() == 0) {  
  139.                 mLocationErrText.setVisibility(View.GONE);  
  140.                 mListener.onLocationChanged(amapLocation);// 显示系统小蓝点  
  141.             } else {  
  142.                 String errText = "定位失败," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo();  
  143.                 Log.e("AmapErr",errText);  
  144.                 mLocationErrText.setVisibility(View.VISIBLE);  
  145.                 mLocationErrText.setText(errText);  
  146.             }  
  147.         }  
  148.     }  
  149.   
  150.     /** 
  151.      * 激活定位 
  152.      */  
  153.     @Override  
  154.     public void activate(OnLocationChangedListener listener) {  
  155.         mListener = listener;  
  156.         if (mlocationClient == null) {  
  157.             mlocationClient = new AMapLocationClient(this);  
  158.             mLocationOption = new AMapLocationClientOption();  
  159.             //设置定位监听  
  160.             mlocationClient.setLocationListener(this);  
  161.             //设置为高精度定位模式  
  162.             mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);  
  163.             //设置定位参数  
  164.             mlocationClient.setLocationOption(mLocationOption);  
  165.             // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,  
  166.             // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求  
  167.             // 在定位结束后,在合适的生命周期调用onDestroy()方法  
  168.             // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除  
  169.             mlocationClient.startLocation();  
  170.         }  
  171.     }  
  172.   
  173.     /** 
  174.      * 停止定位 
  175.      */  
  176.     @Override  
  177.     public void deactivate() {  
  178.         mListener = null;  
  179.         if (mlocationClient != null) {  
  180.             mlocationClient.stopLocation();  
  181.             mlocationClient.onDestroy();  
  182.         }  
  183.         mlocationClient = null;  
  184.     }  
  185.   
  186.       
  187. }  

0 0
原创粉丝点击