下班了,上个班真他妈的累,不知道咋的了,呵呵。。。
把今天的东西总结起来,赶紧走人。。
效果图:
(1)高德地图 mapabc,支持根据手机位置的变化得到的坐标也是最新位置坐标
需要一个adnroid的mapabc的jar,下载地址:http://code.mapabc.com/
核心代码:
在onCreate()方法里面获得一个locationManager = new LocationManagerProxy(this, getResources().getString(R.string.maps_api_key));对象,maps_api_key去http://code.mapabc.com/这里申请一个即可。
主要的两个方法:
public boolean enableMyLocation() { //启用获得位置的监听
boolean result = true;
if (mLocationListener == null) {
mLocationListener = new LocationListenerProxy(locationManager);
result = mLocationListener.startListening(this,
mLocationUpdateMinTime, mLocationUpdateMinDistance);
}
return result;
}
public void disableMyLocation() {//禁用获得位置的监听
if (mLocationListener != null) {
mLocationListener.stopListening();
}
mLocationListener = null;
}
分别在Activity生命周期方法onResume和onPause方法中执行,然后在
public void onLocationChanged(Location location) {
if (location != null) {
Double geoLat = location.getLatitude();
Double geoLng = location.getLongitude();
myLocation.setText("定位成功:(" + geoLng + "," + geoLat + ")");
}
}
方法中获得最新的经纬度(位置信息),然后得到之后想干嘛就干嘛。
最后贴上获得位置的监听器代码
LocationListenerProxy.java
package com.cmmobi.movie.filmdetail;
import com.mapabc.mapapi.LocationManagerProxy;
import com.mapabc.mapapi.LocationProviderProxy;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
public class LocationListenerProxy implements LocationListener {
private final LocationManagerProxy mLocationManager;
private LocationListener mListener = null;
public LocationListenerProxy(final LocationManagerProxy pLocationManager) {
mLocationManager = pLocationManager;
}
public boolean startListening(final LocationListener pListener, final long pUpdateTime,
final float pUpdateDistance) {
boolean result = false;
mListener = pListener;
//获取当前可用的Provider,其中MapABCNetwork为MapABC网络定位(基站和WiFi)
for (final String provider : mLocationManager.getProviders(true)) {
if (LocationManagerProxy.GPS_PROVIDER.equals(provider)||LocationProviderProxy.MapABCNetwork.equals(provider)) {
result = true;
mLocationManager.requestLocationUpdates(provider, pUpdateTime, pUpdateDistance,
this);
}
}
return result;
}
public void stopListening() {
mListener = null;
mLocationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(final Location location) {
if (mListener != null) {
mListener.onLocationChanged(location);
}
}
@Override
public void onProviderDisabled(final String arg0) {
if (mListener != null) {
mListener.onProviderDisabled(arg0);
}
}
@Override
public void onProviderEnabled(final String arg0) {
if (mListener != null) {
mListener.onProviderEnabled(arg0);
}
}
@Override
public void onStatusChanged(final String arg0, final int arg1, final Bundle arg2) {
if (mListener != null) {
mListener.onStatusChanged(arg0, arg1, arg2);
}
}
}
(2)使用android本地api获得自定位信息
public static List<Double> getLocationPosition(Context context){
List<Double> list = new ArrayList<Double>();
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
// Location location = locationManager.getLastKnownLocation(provider);
Location location =getLocation(context);
if(location!=null){
list.add(location.getLongitude());
}
if(location!=null){
list.add(location.getLatitude());
}
return list;
}
/**
* 创建一个LocationListener接口的实现类
*/
public static LocationListener mLocationListener01 = new LocationListener() {
@Override
public void onLocationChanged(Location location) {}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
/**
* 获得Location对象
* @param context
* @return
*/
public static Location getLocation(Context context) {
// LocationManager locMan = (LocationManager) context
// .getSystemService(Context.LOCATION_SERVICE);
LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,
10, mLocationListener01);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10,
mLocationListener01);
Location location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null) {
location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
return location;
}
这种方法的缺点是:不能根据位置的变化而变化,如果要自己实现的话就需要用一个定时器定时的去获得位置信息,也是很简单的了。
好了,下班走了,有问题欢迎讨论。