在Android的快速和频繁的位置更新..? [英] Fast and Frequent location Update in android..?

查看:198
本文介绍了在Android的快速和频繁的位置更新..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void getUserLocation() {

    Location location;
    TextView lon = (TextView) findViewById(R.id.textView2);
    TextView lat = (TextView) findViewById(R.id.textView3);
    boolean GpsEnable = false, NetworkEnabled = false;
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);




    // String locationProvider = LocationManager.GPS_PROVIDER;

    GpsEnable = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    NetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    // locationManager.requestLocationUpdates(locationProvider,0,0,locationListner);

    if (!GpsEnable && !NetworkEnabled) {
        Toast.makeText(getBaseContext(), "No Provider Availabe",
                Toast.LENGTH_SHORT);

    } else {

        if (NetworkEnabled)
            Toast.makeText(getBaseContext(), "Network Provider Available",
                    Toast.LENGTH_SHORT);
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, this);

        if (locationManager != null) {
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            lon.setText("Latitude" + location.getLatitude());
            lat.setText("Longitude " + location.getLongitude());
        }

        if (GpsEnable) {

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, this);

            if (locationManager != null) {

                location = locationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location != null) {
                    lon.setText("Latitude" + location.getLatitude());
                    lat.setText("Longitude " + location.getLongitude());

                }
            }
        }
    }
}

我曾与全球定位系统和网络提供商都做过。我想知道我们如何在谷歌地图得到确切的当前位置?有什么方法或算法由我可以在内部使用谷歌地图我的位置的经度和纬度?
由于提前

I had done both with GPS and network provider.. I want to know how we get exact current location in Google maps? Is there any way or algorithm by which i can get longitude and latitude of my location using internally Google maps? Thanks in Advance

推荐答案

下面是code,我基本上用使用谷歌Play服务获得固定位置的信号。

Here is the code that I basically use to get a constant location signal using Google Play Services.

public class MyActivity
extends
    Activity
implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener,
    LocationListener
{
    private LocationClient locClient;
    private LocationRequest locRequest;
    // Flag that indicates if a request is underway.
    private boolean servicesAvailable = false;


    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        // Check that Google Play services is available
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            servicesAvailable = true;
        } else {

            servicesAvailable = false;
        }

        if(locClient == null) {
            locClient = new LocationClient(this, this, this);
        }

        if(!locClient.isConnected() || !locClient.isConnecting())
        {
            locClient.connect();
        }

        // Create the LocationRequest object
        locRequest = LocationRequest.create();
        // Use high accuracy
        locRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locRequest.setInterval(INTERVAL);
        locRequest.setFastestInterval(INTERVAL);
    }

    @Override
    public void onLocationChanged( Location location )
    {
        // DO SOMETHING WITH THE LOCATION
    }

    @Override
    public void onConnectionFailed( ConnectionResult arg0 )
    {
    }

    @Override
    public void onConnected( Bundle arg0 )
    {
        // Request location updates using static settings
        locClient.requestLocationUpdates(locRequest, this);
    }

    @Override
    public void onDisconnected()
    {
        if(servicesAvailable && locClient != null) {
            //
            // It looks like after a time out of like 90 minutes the activity
            // gets destroyed and in this case the locClient is disconnected and
            // calling removeLocationUpdates() throws an exception in this case.
            //
            if (locClient.isConnected()) {
                locClient.removeLocationUpdates(this);
            }
            locClient = null;
        }
    }
}

这是问题的症结所在反正我这扑杀从其他来源,所以我真的不能要求它但它是。

This is the crux of it anyway and I culled this from other sources so I can't really claim it but there it is.

这篇关于在Android的快速和频繁的位置更新..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆