赞
踩
推荐资源站:https://zhimalier.com/
高德地图目前的定位类型有 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 布局如下:
-
<?xml version=
"1.0" encoding=
"utf-8"?>
-
<FrameLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
-
android:layout_width=
"match_parent"
-
android:layout_height=
"match_parent"
-
android:orientation=
"vertical" >
-
-
<com.amap.api.maps.MapView
-
android:id=
"@+id/map"
-
android:layout_width=
"match_parent"
-
android:layout_height=
"match_parent" >
-
</com.amap.api.maps.MapView>
-
-
<TextView
-
android:id=
"@+id/location_errInfo_text"
-
android:layout_width=
"match_parent"
-
android:layout_height=
"wrap_content"
-
android:layout_gravity=
"bottom|left"
-
android:layout_marginBottom=
"10dp"
-
android:layout_marginLeft=
"10dp"
-
android:background=
"@color/red"
-
android:textColor=
"@color/darkgrey"
-
android:text=
"TextView"
-
android:visibility=
"gone"/>
-
-
<RadioGroup
-
android:id=
"@+id/gps_radio_group"
-
android:layout_width=
"wrap_content"
-
android:layout_height=
"wrap_content"
-
android:layout_gravity=
"top|left"
-
android:layout_marginLeft=
"10dp"
-
android:layout_marginTop=
"10dp"
-
android:background=
"@color/grey"
-
android:orientation=
"horizontal" >
-
-
<RadioButton
-
android:id=
"@+id/gps_locate_button"
-
android:layout_width=
"wrap_content"
-
android:layout_height=
"wrap_content"
-
android:checked=
"true"
-
android:text=
"@string/gpslocate"
-
android:textColor=
"@android:color/black" />
-
-
<RadioButton
-
android:id=
"@+id/gps_follow_button"
-
android:layout_width=
"wrap_content"
-
android:layout_height=
"wrap_content"
-
android:text=
"@string/gpsfollow"
-
android:textColor=
"@android:color/black" />
-
-
<RadioButton
-
android:id=
"@+id/gps_rotate_button"
-
android:layout_width=
"wrap_content"
-
android:layout_height=
"wrap_content"
-
android:text=
"@string/gpsrotate"
-
android:textColor=
"@android:color/black" />
-
</RadioGroup>
-
-
</FrameLayout>
-
package com.amap.map3d.demo;
-
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.Window;
-
import android.widget.RadioGroup;
-
import android.widget.RadioGroup.OnCheckedChangeListener;
-
import android.widget.TextView;
-
-
import com.amap.api.location.AMapLocation;
-
import com.amap.api.location.AMapLocationClient;
-
import com.amap.api.location.AMapLocationClientOption;
-
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
-
import com.amap.api.location.AMapLocationListener;
-
import com.amap.api.maps.AMap;
-
import com.amap.api.maps.LocationSource;
-
import com.amap.api.maps.MapView;
-
import com.amap.map3d.demo.R;
-
-
public
class MainActivity extends Activity implements LocationSource,
-
AMapLocationListener,
OnCheckedChangeListener {
-
private AMap aMap;
-
private MapView mapView;
-
// 处理定位更新
-
private OnLocationChangedListener mListener;
-
// 定位
-
private AMapLocationClient mlocationClient;
-
-
private AMapLocationClientOption mLocationOption;
-
private RadioGroup mGPSModeGroup;
-
-
private TextView mLocationErrText;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 不显示程序的标题栏
-
setContentView(R.layout.activity_main);
-
mapView = (MapView) findViewById(R.id.map);
-
mapView.onCreate(savedInstanceState);
// 此方法必须重写
-
init();
-
}
-
-
/**
-
* 初始化
-
*/
-
private void init() {
-
if (aMap ==
null) {
-
aMap = mapView.getMap();
-
setUpMap();
-
}
-
mGPSModeGroup = (RadioGroup) findViewById(R.id.gps_radio_group);
-
mGPSModeGroup.setOnCheckedChangeListener(
this);
-
mLocationErrText = (TextView)findViewById(R.id.location_errInfo_text);
-
mLocationErrText.setVisibility(View.GONE);
-
}
-
-
/**
-
* 设置一些amap的属性
-
*/
-
private void setUpMap() {
-
aMap.setLocationSource(
this);
// 设置定位监听
-
aMap.getUiSettings().setMyLocationButtonEnabled(
true);
// 设置默认定位按钮是否显示
-
aMap.setMyLocationEnabled(
true);
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
-
// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
-
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
-
}
-
-
@Override
-
public void onCheckedChanged(RadioGroup group, int checkedId) {
-
switch (checkedId) {
-
case R.id.gps_locate_button:
-
// 设置定位的类型为定位模式
-
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
-
break;
-
case R.id.gps_follow_button:
-
// 设置定位的类型为 跟随模式
-
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
-
break;
-
case R.id.gps_rotate_button:
-
// 设置定位的类型为根据地图面向方向旋转
-
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
-
break;
-
}
-
-
}
-
-
/**
-
* 方法必须重写
-
*/
-
@Override
-
protected void onResume() {
-
super.onResume();
-
mapView.onResume();
-
}
-
-
/**
-
* 方法必须重写
-
*/
-
@Override
-
protected void onPause() {
-
super.onPause();
-
mapView.onPause();
-
deactivate();
-
}
-
-
/**
-
* 方法必须重写
-
*/
-
@Override
-
protected void onSaveInstanceState(Bundle outState) {
-
super.onSaveInstanceState(outState);
-
mapView.onSaveInstanceState(outState);
-
}
-
-
/**
-
* 方法必须重写
-
*/
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
mapView.onDestroy();
-
if(
null != mlocationClient){
-
mlocationClient.onDestroy();
-
}
-
}
-
-
/**
-
* 定位成功后回调函数
-
*/
-
@Override
-
public void onLocationChanged(AMapLocation amapLocation) {
-
if (mListener !=
null && amapLocation !=
null) {
-
if (amapLocation !=
null
-
&& amapLocation.getErrorCode() ==
0) {
-
mLocationErrText.setVisibility(View.GONE);
-
mListener.onLocationChanged(amapLocation);
// 显示系统小蓝点
-
}
else {
-
String errText =
"定位失败," + amapLocation.getErrorCode()+
": " + amapLocation.getErrorInfo();
-
Log.e(
"AmapErr",errText);
-
mLocationErrText.setVisibility(View.VISIBLE);
-
mLocationErrText.setText(errText);
-
}
-
}
-
}
-
-
/**
-
* 激活定位
-
*/
-
@Override
-
public void activate(OnLocationChangedListener listener) {
-
mListener = listener;
-
if (mlocationClient ==
null) {
-
mlocationClient =
new AMapLocationClient(
this);
-
mLocationOption =
new AMapLocationClientOption();
-
//设置定位监听
-
mlocationClient.setLocationListener(
this);
-
//设置为高精度定位模式
-
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
-
//设置定位参数
-
mlocationClient.setLocationOption(mLocationOption);
-
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
-
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
-
// 在定位结束后,在合适的生命周期调用onDestroy()方法
-
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
-
mlocationClient.startLocation();
-
}
-
}
-
-
/**
-
* 停止定位
-
*/
-
@Override
-
public void deactivate() {
-
mListener =
null;
-
if (mlocationClient !=
null) {
-
mlocationClient.stopLocation();
-
mlocationClient.onDestroy();
-
}
-
mlocationClient =
null;
-
}
-
-
-
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。