高德地图定位
代码下载地址:http://download.csdn.net/detail/u011324501/9439438
可以实现定位、追随、跟旋转功能。应该代码比较少,实现基本的定位功能。
可以到高德官网先申请个key:http://lbs.amap.com/console/key/
申请好后添加到AndroidManifest.xml文件替换原来的key
1、需要添加下面图片的.so跟.jar文件。可以去官网下载例程。
2、用到的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
key:
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="8c2df4e8c0b9c5598463cc525258b846" />
3、布局文件: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>
4、主代码:MainActivity.java
package com.example.mapgaode;
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;
/**
* AMapV2地图中介绍定位三种模式的使用,包括定位,追随,旋转
*/
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.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;
}
}
代码下载地址:http://download.csdn.net/detail/u011324501/9439438
最后实现的效果: