【问题标题】:Fast and Frequent location Update in android..?android.. 中快速且频繁的位置更新?
【发布时间】:2013-12-15 05:34:51
【问题描述】:
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());

                }
            }
        }
    }
}

我已经使用 GPS 和网络提供商。我想知道我们如何在 Google 地图中获得准确的当前位置?有没有什么方法或算法可以让我使用内部谷歌地图获得我所在位置的经度和纬度? 提前致谢

【问题讨论】:

    标签: android multithreading performance maps


    【解决方案1】:

    我已按照本教程快速更新和初始设置 GoogleMap v2

    Initial Setup Here

    Alternative for LocationUpdate

    希望这可以帮助...:)

    【讨论】:

      【解决方案2】:
      public void retriveLocation(){
          try {
              String locCtx = Context.LOCATION_SERVICE;
              LocationManager locationmanager = (LocationManager) context.getSystemService(locCtx);           
               Criteria criteria = new Criteria();
               criteria.setAccuracy(Criteria.ACCURACY_FINE);
               criteria.setAltitudeRequired(false);
               criteria.setBearingRequired(false);
               criteria.setPowerRequirement(Criteria.POWER_LOW);
               String provider = locationmanager.getBestProvider(criteria, true);
               locationmanager.requestLocationUpdates(provider, 0, 0, this);
          } catch (Exception e) {         
          }
      
      }
      

      希望此代码可用于检索快速位置更新。

      【讨论】:

        【解决方案3】:

        这是我基本上用来使用 Google Play 服务获取恒定位置信号的代码。

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

        无论如何,这就是它的症结所在,我从其他来源中挑选了它,所以我不能真正声称它,但它确实存在。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-08
          • 1970-01-01
          相关资源
          最近更新 更多