码迷,mamicode.com
首页 > 其他好文 > 详细

高德定位

时间:2016-07-16 11:30:34      阅读:881      评论:0      收藏:0      [点我收藏+]

标签:

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
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 java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements LocationSource,AMapLocationListener,
CompoundButton.OnCheckedChangeListener {

private MapView mMapView;
private AMap aMap;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
private OnLocationChangedListener mListener;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);

//设置监听
aMap = mMapView.getMap();
aMap.setLocationSource(this);
aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认按钮是否显示
aMap.setMyLocationEnabled(true);
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);//设置定位的模式为定位模式


}


@Override
public void onLocationChanged(AMapLocation aMapLocation) {
Log.i("TAG","onLocationChanged"+1);

if (mListener != null && aMapLocation != null) {
if (aMapLocation != null
&& aMapLocation.getErrorCode() == 0) {
Log.i("TAG", "定位成功");
// Toast.makeText(MainActivity.this, "定位成功", Toast.LENGTH_LONG).show();
mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
} else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();

Toast.makeText(MainActivity.this, errText, Toast.LENGTH_LONG).show();
//Log.e("AmapErr", errText);
}
}

}

@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
Log.i("TAG","activate"+2);
mListener = onLocationChangedListener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
}
}

@Override
public void deactivate() {
Log.i("TAG","deactivate"+3);
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Log.i("TAG","onCheckedChanged"+4);
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}

public String sHA1(Context context) {
Log.i("TAG","sHA1"+5);
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
context.getPackageName(), PackageManager.GET_SIGNATURES);
byte[] cert = info.signatures[0].toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] publicKey = md.digest(cert);
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < publicKey.length; i++) {
String appendString = Integer.toHexString(0xFF & publicKey[i])
.toUpperCase(Locale.US);
if (appendString.length() == 1)
hexString.append("0");
hexString.append(appendString);
hexString.append(":");
}
return hexString.toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPause() {
super.onPause();

Log.i("TAG","onPause"+6);
mMapView.onPause();
deactivate();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("TAG","onSaveInstanceState"+7);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
super.onResume();
Log.i("TAG","onResume"+8);
mMapView.onResume();
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("TAG","onDestroy"+9);
mMapView.onDestroy();
mMapView.onDestroy();
if (null != mlocationClient) {
mlocationClient.onDestroy();
}
}


}
//mainfest文件 还需要导入地图的3djar 和定位的Jar key需要自己申请 应用的包名和项目的包名需一致
<?xml version="1.0" encoding="utf-8"?>
<manifest package="itcast.cn.mymap"
xmlns:android="http://schemas.android.com/apk/res/android">


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.amap.api.v2.apikey"
android:value="3e38e8decba8f7e2f3909e76ac82362e"
></meta-data>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<service android:name="com.amap.api.location.APSService"></service>
</application>
//xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
>

<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.amap.api.maps.MapView>



</RelativeLayout>


高德定位

标签:

原文地址:http://www.cnblogs.com/l123/p/5675477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!