在gps上识别位置的最佳方法是什么?

时间:2014-12-29 23:18:18

标签: android gps

我正在尝试用gps开发应用程序,可以知道我是否到达或离开特定的地方。 I.E如果我在家,应用程序知道,如果我离开家,应用程序也知道。或者如果我想设置我家的半径。但我认为问题在于,当我尝试运行应用程序并每隔x次搜索一次就可以杀死我的电池。

该行动的最佳解决方案是什么?我也应该使用服务吗? 这是我为获得这个地方和坐标所做的课程:

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;
    }


}
抱歉我的英语。希望你理解我:)。

1 个答案:

答案 0 :(得分:0)

这取决于您所需的准确性。 如果所需的范围大约是1 - 3km,那么就有节电解决方案,我在此不再赘述。 (使用设备Network locationg procider) 为了更好的准确性,总是无法使用GPS, 在我的iPhone 4上持续约8小时。

检测是否通过进入由lat,lon,radius定义的圆内部来完成到达。

内圈计算方法如下:

boolean isInside = currentLocation.distanceTo(circleCenterLongitude, circleCenterLatitude) < radiusMeters.