项目名字:AndTripLog-旅游日记
一个很好的学习定位的开源项目,用的是google的定位系统,
但是google的基站定位在国内很不稳定,基本处于不能用的状态
本着自己动手丰衣足食的精神,果断改成百度的定位系统,
写了一个百度定位的工具类:
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
import com.baidu.location.*;
public class LocationUtils {
private Context context;
private boolean isLocationupdate=false;
private String locationInfo;
private Handler mhandler;
private BDLocation bdLocation;
public LocationClient mLocationClient = null;
public MyLocationListenner myListener = new MyLocationListenner();
public LocationUtils(Context context,Handler myhandler){
this.context=context;
mhandler=myhandler;
}
class MyLocationListenner implements BDLocationListener{
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
Log.d("AndTripLog","onReceiveLocation");
if (location == null)
return ;
Log.d("AndTripLog","onReceiveLocation is not null");
bdLocation=location;
isLocationupdate=true;
locationInfo="Latitude:"+location.getLatitude()+",Longitude:"+location.getLongitude();
mhandler.sendEmptyMessage(0);
}
public void onReceivePoi(BDLocation poiLocation) {
// TODO Auto-generated method stub
if (poiLocation == null){
return ;
}
}
}
public String getLocationInfo(){
return locationInfo;
}
public BDLocation getLocation(){
return bdLocation;
}
public void startLocation(){
isLocationupdate=false;
mLocationClient = new LocationClient(context);
setLocationOption();
mLocationClient.registerLocationListener(myListener);
Log.d("lostprevent","startLocation");
mLocationClient.start();
}
public boolean isupdate(){
return isLocationupdate;
}
public void stopLocation(){
isLocationupdate=false;
mLocationClient.stop();
}
private void setLocationOption(){
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setServiceName("com.baidu.location.service_v3.3");
option.setPoiExtraInfo(false);
option.setAddrType("all");
option.setScanSpan(3000);
option.setCoorType("bd09ll");
option.setPriority(LocationClientOption.NetWorkFirst);
option.disableCache(true);
mLocationClient.setLocOption(option);
}
}
相应的权限设置、需要添加的包见官方的介绍:
http://developer.baidu.com/map/geosdk-android-developv3.3.htm
通过此项目学习,可以很好的学习知识点:
1.定位系统
2.SQLite语句
3.文件保存
4.邮件发送
项目源码下载地址:
http://download.youkuaiyun.com/detail/txj8612/5353448
android默认定位系统类LocationManager:
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
provider:指定用定位的类型:GPS_PROVIDER为GPS定位,NETWORK_PROVIDER为基站定位
minTime:最小定位间隔时间
minDistance:最小定位间隔距离
listener:定位监听器,实现了接口LocationListener的四个抽象方法:
abstract void onLocationChanged(Location location)
//Called when the location has changed.
abstract void onProviderDisabled(String provider)
//Called when the provider is disabled by the user.
abstract void onProviderEnabled(String provider)
//Called when the provider is enabled by the user.
abstract void onStatusChanged(String provider, int status, Bundle extras)
//Called when the provider status changes.
定位的信息从onLocationChanged(Location location)中的location中获取。