0
我正在嘗試開發可以知道我是否到達或離開特定地點的gps應用程序。 I.E如果我在家,應用程序知道,如果我離開我的家,應用程序也知道。或者如果我想設置我家的半徑。但我認爲問題在於,當我嘗試每x次運行應用程序和搜索位置時,它可能會導致我的電池無法使用。什麼是識別gps地點的最佳方式?
什麼是該行動的最佳解決方案?我應該使用服務嗎? 這裏是我做得到的地方,座標類:
public class LocationMap extends Service implements LocationListener {
public static final String LOG = "locationLogger";
Context context;
LocationManager manager;
Location location;
public static final int REFRESH = 1000*1;
public static final int DISTANCE = 1*1;
double lat,lng;
boolean isGps,isWiFi;
public LocationMap(Context context) {
this.context = context;
getLocation();
}
private Location getLocation(){
try {
manager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,REFRESH,DISTANCE,this);
//Check who is on WiFi or GPS
isWiFi = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);Log.d(LOG, "Wifi?="+isWiFi);
isGps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);Log.d(LOG, "Gps?="+isGps);
Log.d(LOG, "manager is not null?="+manager);
if (manager != null) {
if(isGps){
Log.d(LOG, "get last location from Gps");
location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else
if(isWiFi){
Log.d(LOG, "get last location from WiFi");
location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Log.d(LOG, "lat="+lat +" lng="+lng);
}
}
} catch (Exception e) {
}
return location;
}
public String getPlace(){
String placeName = "No place found, check your gps setting";
try {
Geocoder geocoder = new Geocoder(context);
List<Address> address =geocoder.getFromLocation(getLat(), getLng(), 1);
String country = address.get(0).getCountryName();
String city = address.get(0).getLocality();
String street = address.get(0).getAddressLine(0);
placeName = country+", "+city+", "+street;
} catch (IOException e) {
e.printStackTrace();
}
return placeName;
}
public double getLng(){
return lng;
}
public double getLat() {
return lat;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
對不起我的英語。希望你能理解我:)
可搜索的關鍵字是 「地理圍欄」:
圓內由下式計算。 –
在哪裏我可以找到最好的教程,我搜索它,它看起來正是我需要的!謝謝 –
哪個教程最好是非常主觀的。只有你可以決定哪一個對你說話。 –