2014-12-29 46 views
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; 
    } 


} 

對不起我的英語。希望你能理解我:)

+0

可搜索的關鍵字是 「地理圍欄」:

圓內由下式計算。 –

+0

在哪裏我可以找到最好的教程,我搜索它,它看起來正是我需要的!謝謝 –

+0

哪個教程最好是非常主觀的。只有你可以決定哪一個對你說話。 –

回答

0

這取決於你需要的準確性。 如果希望的範圍大約爲1 - 3km,那麼就有節電解決方案,我不會在這裏進一步解釋。 (使用設備網絡locationg procider) 爲了更好的精度,沒有辦法使用GPS始終, ,這在我的iphone 4持續約8小時。

檢測是否通過簡單地通過輸入在經緯度,半徑,半徑定義的圓內完成檢測。

boolean isInside = currentLocation.distanceTo(circleCenterLongitude, circleCenterLatitude) < radiusMeters. 
相關問題