Android GPS需要太多时间才能获得位置坐标

Android GPS需要太多时间才能获得位置坐标,第1张

概述Here, GPS takes half an hour or more (time) to get the current location coordinates. How can I use GPS from GPS_SATELLITES and GPS _NETWORK_PROVIDERS simultaneously in the same context and get the val

Here,GPS takes half an hour or more (time) to get the current location coordinates.

How can I use GPS from GPS_SATELliTES and GPS _NETWORK_PROVIDERS
simultaneously in the same context and get the value of the recent
GPS?

public class Imagefile extends Activity{    LocationListener Listner;Location location;LocationManager locationManager ;private static final long MIN_disTANCE_CHANGE_FOR_UPDATES = 10; // 10 metersprivate static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 1 minute@OverrIDeprotected voID onCreate(final Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.branchreport_layout);    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    Listner = new MyLocationListner();    isGPSEnabled = locationManager.isProvIDerEnabled(LocationManager.GPS_PROVIDER);    isNetworkEnabled = locationManager.isProvIDerEnabled(LocationManager.NETWORK_PROVIDER);    if(isGPSEnabled==false&&isNetworkEnabled==false)    {        showSettingsAlert();    }    if (isGPSEnabled)     {        progress = ProgressDialog.show(this,"GPS","Fetching latitude and longitude...",true);        locationManager.requestLocationUpdates(                LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_disTANCE_CHANGE_FOR_UPDATES,Listner);       }    else if (isNetworkEnabled)    {        progress = ProgressDialog.show(this,true);        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,Listner);    }public class MyLocationListner implements LocationListener {    @TargetAPI(Build.VERSION_CODES.GINGERBREAD)    @OverrIDe    public voID onLocationChanged(Location location) {        // Todo auto-generated method stub        x = location.getLatitude();        y = location.getLongitude();        if((x!=0) && (y!=0))        {            progress.dismiss();            locationManager.removeUpdates(Listner);            Geocoder gCoder = new Geocoder(Imagefile.this);            try {                addresses = gCoder.getFromLocation(x,y,1);            } catch (IOException e) {                // Todo auto-generated catch block                e.printstacktrace();            }            if (addresses != null && addresses.size() > 0) {                Log.d("TestTag","Locality: " + addresses.get(0).getLocality()+"getAddressline"+addresses.get(0).getAddressline(0)+",getadminArea"+addresses.get(0).getadminArea()+",getSubLocality"+addresses.get(0).getSubLocality());            }        }    }    @OverrIDe    public voID onProvIDerDisabled(String arg0) {        // Todo auto-generated method stub    }    @OverrIDe    public voID onProvIDerEnabled(String arg0) {        // Todo auto-generated method stub    }    @OverrIDe    public voID onStatusChanged(String arg0,int arg1,Bundle arg2) {        // Todo auto-generated method stub    }}public voID showSettingsAlert(){    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);    // 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);            startActivity(intent);        }    });    // on pressing cancel button    alertDialog.setNegativebutton("Cancel",int which) {            dialog.cancel();        }    });    alertDialog.show();}

}

解决方法 GPS总是很慢,因此我建议您使用Google Play服务使用AndroID Location API.

脚步

First check for availability of Google Play Services by calling in onResume() of the Activity

Once play services are available on the device,build the Googleapiclient.

Connect to Google Api clIEnt by calling mGoogleapiclient.connect() in onStart() method. By calling this,onConnectionFailed(),onConnected() and onConnectionSuspended() (GooglePlayServiceListeners) will be triggered depending upon the connection status.

Once Google Api is successfully connected,displayLocation() should be called in onConnected() method to get the current location.

这就是我们构建Googleapiclient的方式

protected synchronized voID buildGoogleapiclient() {        mGoogleapiclient = new Googleapiclient.Builder(this)                .addConnectionCallbacks(this)                .addOnConnectionFailedListener(this)                .addAPI(LocationServices.API).build();    }

displayLocation()

private voID displayLocation() {        mLastLocation = LocationServices.FusedLocationAPI                .getLastLocation(mGoogleapiclient);        if (mLastLocation != null) {            double latitude = mLastLocation.getLatitude();            double longitude = mLastLocation.getLongitude();            lblLocation.setText(latitude + "," + longitude);        } else {            lblLocation                    .setText("(Couldn't get the location. Make sure location is enabled on the device)");        }    }

上述方法和步骤的完美解释可以在here找到.也可以查看官方documentation

总结

以上是内存溢出为你收集整理的Android GPS需要太多时间才能获得位置坐标全部内容,希望文章能够帮你解决Android GPS需要太多时间才能获得位置坐标所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1127840.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存