高德定位
要想实现定位功能 首先需要从高德的官网申请一个key 下载 AMap_Location_v1.4.1_20150917.jar 包 把该包放在所建android 项目的bins文件里面。
在AndroidManifest.xml 进行配置 把下面代码加入application 中
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="83c4f8b7b8f4ef6b80e6d01dfbbc1d29" />
83c4f8b7b8f4ef6b80e6d01dfbbc1d29 是你申请的key
AndroidManifest.xml 中的权限配置代码
<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.WRITE_SETTINGS" />
布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
java代码
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.example.map.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
public class Location extends Activity implements AMapLocationListener {
LocationManagerProxy mLocationManagerProxy;
private TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.location);
tv = (TextView) findViewById(R.id.tv);
// 初始化定位对象
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
// 定位注册
// 第一个参数是定位类型:高德定位,混合类型包括GPS和网络定位,哪个先返回就采用哪个定位
// ,同时返回的话优先GPS定位。第二个参数是定位周期
// 单位毫秒。第三个参数是对GPS有用
// 移动多大距离从新定位,
mLocationManagerProxy.requestLocationData(
LocationProviderProxy.AMapNetwork, 2000, 15, this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationManagerProxy.destroy();
}
@Override
public void onLocationChanged(android.location.Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(AMapLocation arg0) {
// 定位回调
if (arg0 != null && arg0.getAMapException().getErrorCode() == 0) {
Log.d("hello world", arg0.toString());
tv.setText(arg0.toString());
}
}
}