刚接触android,照着网上例子做了个定位的小程序,结果定不了位,帮忙看下什么问题
private GeoPoint getCurrentGeoPoint() {
GeoPoint p=null;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria =new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Toast.makeText(this,provider,Toast.LENGTH_LONG).show();
Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location ==null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener );
location =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(location==null){
p=new GeoPoint((int)(23.129803*1e6),(int)(113.219218*1e6));
}
else
{
double lat=location.getLatitude();
double lng=location.getLongitude();
p=new GeoPoint((int)(lat*1e6),(int)(lng*1e6));
}
return p;
}
private LocationListener locationListener =new LocationListener(){
//当坐标改变时触发此函数
publicvoid onLocationChanged(Location location){}
//Provider禁用时触发此函数,比如GPS被关闭
publicvoid onProviderDisabled(String provider) {}
//Provider启用时触发此函数,比如GPS被打开
publicvoid onProviderEnabled(String provider){}
//Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
publicvoid onStatusChanged(String provider,int status,Bundle extras){}
};