Android 安卓&x27;世界上最精确的定位API

Android 安卓&x27;世界上最精确的定位API,android,location,Android,Location,为了工作,我的应用程序需要一个位置API。 我打算使用Mapbox平台来定制它的设计(因为Google地图) 就我而言,不提供这种定制级别) 文档中说,在构建位置时,我们应该使用GooglePlayAPI 应用程序: Google Play服务位置API优于Android 框架位置API(android.location)作为添加 应用程序的位置感知。如果您当前正在使用Android 框架位置API,强烈建议您切换到 谷歌播放服务位置API尽快 我的问题是: Google Play API是GP

为了工作,我的应用程序需要一个位置API。 我打算使用Mapbox平台来定制它的设计(因为Google地图) 就我而言,不提供这种定制级别)

文档中说,在构建位置时,我们应该使用GooglePlayAPI 应用程序:

Google Play服务位置API优于Android 框架位置API(android.location)作为添加 应用程序的位置感知。如果您当前正在使用Android 框架位置API,强烈建议您切换到 谷歌播放服务位置API尽快

我的问题是: Google Play API是GPS精度方面最有效的API吗? 或者我应该使用LocationManager和LocationListener的方法来做吗

我需要准确。我应该用哪一个? 谢谢。

使用并将LocationRequest优先级设置为

这是最新的API,用于精确的位置获取,谷歌建议使用相同的API

检查准确性细节


基本上,Google play services API具有通过融合GPS+网络提供商+被动提供商获得准确位置的智能。

在android中,还有三种类型的位置:

  • 全球定位系统供应商
  • 网络供应商
  • 被动_提供者
  • 因此,根据我的编码经验,我知道如果您使用:

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new MyLocationListener());
    
    您将获得高达14位以上的精度

    但如果你像这样使用它们的融合:

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, my_google_listener);
    
    您将获得高达小数点后6到7位的精度。试试看

    但请注意,GPS提供商获取位置需要时间,而Google location从API调用获取数据到Google服务器数据库的速度要快得多


    GPS离线工作,而google provider通过移动或wifi数据获取位置。

    根据我的经验,google Services location API最好用于以下方面:

    • 他们处理用户设置,选择最佳可用位置提供商。如果您选择直接使用LocationManager,您的代码将需要处理该问题
    • 你可能会期望用更少的电力获得更好的位置,因为谷歌定期更新他们使用WiFi、手机发射塔等确定位置的算法
    根据我的经验,使用谷歌服务,在许多情况下,精确到足以用于地图应用程序(大约几十米)的位置不需要GPS数据。FusedLocationProvider也可能是这种情况,但电池使用数量可能是个例外


    总之,如果您没有理由不使用谷歌服务(例如-您的目标国家没有谷歌服务,或者希望通过其他市场进行分销),那么您应该使用他们的位置API。

    为了准确起见,您应该使用LocationManager。 也可以使用这个类

    //GPSTracker.java
    import android.app.AlertDialog;
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.provider.Settings;
    import android.util.Log;
    
    public class GPSTracker extends Service implements LocationListener {
    
    private final Context mContext;
    
    // flag for GPS status
    boolean isGPSEnabled = false;
    
    // flag for network status
    boolean isNetworkEnabled = false;
    
    // flag for GPS status
    boolean canGetLocation = false;
    
    Location location; // location
    double latitude; // latitude
    double longitude; // longitude
    float bearing; // bearing
    
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    
    // Declaring a Location Manager
    protected LocationManager locationManager;
    
    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }
    
    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);
    
            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            bearing = location.getBearing();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                bearing = location.getBearing();
                            }
                        }
                    }
                }
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return location;
    }
    
    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }
    
    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
    
        // return latitude
        return latitude;
    }
    
    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
    
        // return longitude
        return longitude;
    }
    
    public float getBearing() {
        if (location != null) {
            bearing = location.getBearing();
        }
        return bearing;
    }
    
    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }
    
    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
    
        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");
    
        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
    
        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });
    
        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    
        // Showing Alert Message
        alertDialog.show();
    }
    
    @Override
    public void onLocationChanged(Location location) {
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    }
    
    //MainActivity.java
    GPSTracker gps = new GPSTracker(MainActivity.this);
    
    已弃用。您应该使用并添加
    ACCESS\u FINE\u LOCATION
    权限,而不是
    ACCESS\u rough\u LOCATION
    ,以获得最准确的位置


    阅读这篇文章,了解为什么FusedLocationProviderClient是最好的解决方案。

    所以,如果它是准确的,但会耗尽电池电量,您对此是否满意?首先,是的!这就是他们推荐Google Play API的原因吗?我认为
    fusedlocation
    (又称Google location API)的设计在电池使用和网络方面更高效。我没有看到任何基准,所以这都是主观和推测的;我从
    fusedlocation
    获得的精确度已经足够了,除非它表现得很糟糕,否则我不会尝试任何东西。@muratgu米的精确度有多高?fusedlocation使用网络和GPS提供的精确度比GPS要低,但耗电更少。它的准确性取决于很多因素。GPS alon的精度约为5-10米,但取决于手机硬件、清晰的视线、大气条件等。融合定位精度较低,并且具有许多相同的条件。网络将是最小的,比如说200米以内,但几乎不使用电力。融合和网络都需要网络连接(GPS不需要)。但是-小数点计数与实际位置精度相关吗?我猜不会。^是的!,小数点计数会影响定位的准确性。更精确的坐标意味着更精确的位置。没有更精确的坐标并不意味着更精确,它只意味着更多的小数位数。小数位数应根据准确度进行调整,但我看不到任何事实表明这一点。