android.location.LocationManager.getLastKnownLocation()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中android.location.LocationManager.getLastKnownLocation()方法的一些代码示例,展示了LocationManager.getLastKnownLocation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocationManager.getLastKnownLocation()方法的具体详情如下:
包路径:android.location.LocationManager
类名称:LocationManager
方法名:getLastKnownLocation

LocationManager.getLastKnownLocation介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

代码示例来源:origin: stackoverflow.com

Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if (location != null) {
  Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
  mLocationManager.removeUpdates(this);

代码示例来源:origin: stackoverflow.com

lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
  timer1.cancel();
  locationResult.gotLocation(location);
  lm.removeUpdates(this);
  lm.removeUpdates(locationListenerNetwork);
  timer1.cancel();
  locationResult.gotLocation(location);
  lm.removeUpdates(this);
  lm.removeUpdates(locationListenerGps);
     gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
   if(network_enabled)
     net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

代码示例来源:origin: stackoverflow.com

private void _getLocation() {
  // Get the location manager
  LocationManager locationManager = (LocationManager) 
      getSystemService(LOCATION_SERVICE);
  Criteria criteria = new Criteria();
  String bestProvider = locationManager.getBestProvider(criteria, false);
  Location location = locationManager.getLastKnownLocation(bestProvider);
  LocationListener loc_listener = new LocationListener() {

    public void onLocationChanged(Location l) {}

    public void onProviderEnabled(String p) {}

    public void onProviderDisabled(String p) {}

    public void onStatusChanged(String p, int status, Bundle extras) {}
  };
  locationManager
      .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
  location = locationManager.getLastKnownLocation(bestProvider);
  try {
    lat = location.getLatitude();
    lon = location.getLongitude();
  } catch (NullPointerException e) {
    lat = -1.0;
    lon = -1.0;
  }
}

代码示例来源:origin: multidots/android-app-common-tasks

try {
  locationManager = (LocationManager) context
      .getSystemService(context.LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
  System.out.println("gps band chhe" + isGPSEnabled);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
              .getLastKnownLocation(LocationManager.GPS_PROVIDER);

代码示例来源:origin: googlecodelabs/android-lifecycles

void addLocationListener() {
  // Note: Use the Fused Location Provider from Google Play Services instead.
  // https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
  mLocationManager =
      (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
  Log.d("BoundLocationMgr", "Listener added");
  // Force an update with the last location, if available.
  Location lastLocation = mLocationManager.getLastKnownLocation(
      LocationManager.GPS_PROVIDER);
  if (lastLocation != null) {
    mListener.onLocationChanged(lastLocation);
  }
}

代码示例来源:origin: stackoverflow.com

this.isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
this.isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    if (locationManager != null)   {
      location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      updateCoordinates();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      updateCoordinates();

代码示例来源:origin: stackoverflow.com

.getSystemService(Context.LOCATION_SERVICE);
try {
  if (locationManager.isProviderEnabled(provider)) {
    location = locationManager.getLastKnownLocation(provider);

代码示例来源:origin: stackoverflow.com

super.onCreate();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
sendBroadcastMessage(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
    new LocationListener() {
      @Override

代码示例来源:origin: openbmap/radiocells-scanner-android

@Override
  public void run() {
    mLocationManager.removeUpdates(locationListenerGps);
    mLocationManager.removeUpdates(locationListenerNetwork);
    Location net_loc  = null, gps_loc  = null;
    if (mGpsEnabled)
      gps_loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (mNetworkEnabled)
      net_loc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //if there are both values use the latest one
    if (gps_loc != null && net_loc != null){
      if (gps_loc.getTime() > net_loc.getTime())
        mLocationResult.gotLocation(gps_loc);
      else
        mLocationResult.gotLocation(net_loc);
      return;
    }
    if (gps_loc != null) {
      mLocationResult.gotLocation(gps_loc);
      return;
    }
    if (net_loc != null) {
      mLocationResult.gotLocation(net_loc);
      return;
    }
    mLocationResult.gotLocation(null);
  }
}

代码示例来源:origin: stackoverflow.com

try {
  locationManager = (LocationManager) mContext
      .getSystemService(LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      if (locationManager != null) {
        location = locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
          latitude = location.getLatitude();
        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MIN_TIME_BW_UPDATES,
        if (locationManager != null) {
          location = locationManager
              .getLastKnownLocation(LocationManager.GPS_PROVIDER);
          if (location != null) {
            latitude = location.getLatitude();
  locationManager.removeUpdates(GPSTracker.this);

代码示例来源:origin: stackoverflow.com

LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

double latitude=0;
double longitude=0;
latitude = location.getLatitude();
longitude = location.getLongitude();

代码示例来源:origin: multidots/android-app-common-tasks

try {
  locationManager = (LocationManager) context
      .getSystemService(context.LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
  System.out.println("gps band chhe" + isGPSEnabled);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
              .getLastKnownLocation(LocationManager.GPS_PROVIDER);

代码示例来源:origin: googlecodelabs/android-lifecycles

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void addLocationListener() {
  // Note: Use the Fused Location Provider from Google Play Services instead.
  // https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
  mLocationManager =
      (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
  Log.d("BoundLocationMgr", "Listener added");
  // Force an update with the last location, if available.
  Location lastLocation = mLocationManager.getLastKnownLocation(
      LocationManager.GPS_PROVIDER);
  if (lastLocation != null) {
    mListener.onLocationChanged(lastLocation);
  }
}

代码示例来源:origin: stackoverflow.com

Location location = locationManager.getLastKnownLocation(provider);
protected void onResume() {
  super.onResume();
  locationManager.requestLocationUpdates(provider, 400, 1, this);
protected void onPause() {
  super.onPause();
  locationManager.removeUpdates(this);

代码示例来源:origin: facebook/facebook-android-sdk

String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null && checkForLocationPermissionsAndRequest()) {
  location = locationManager.getLastKnownLocation(bestProvider);
  if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) {
    locationListener = new LocationListener() {
      @Override
    locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD,
        locationListener, Looper.getMainLooper());

代码示例来源:origin: stackoverflow.com

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
  map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

  CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
    .zoom(17)                   // Sets the zoom
    .bearing(90)                // Sets the orientation of the camera to east
    .tilt(40)                   // Sets the tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder
  map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));       
}

代码示例来源:origin: stackoverflow.com

.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
  net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

代码示例来源:origin: fossasia/pslab-android

/**
 * @return the best location fetched
 */
@SuppressLint("MissingPermission")
public Location getDeviceLocation() {
  if (bestLocation == null) {
    if (psLabPermission.checkPermissions((Activity) context, PSLabPermission.MAP_PERMISSION)) {
      locationManager.requestLocationUpdates(provider,
          UPDATE_INTERVAL_IN_MILLISECONDS, MIN_DISTANCE_CHANGE_FOR_UPDATES,
          locationListener);
      if (locationReady) {
        return locationManager.getLastKnownLocation(provider);
      } else {
        return dummyLocation();
      }
    } else {
      return dummyLocation();
    }
  } else {
    return bestLocation;
  }
}

代码示例来源:origin: stackoverflow.com

locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  locationManager.requestLocationUpdates(
    provider_info,
    MIN_TIME_BW_UPDATES,
    location = locationManager.getLastKnownLocation(provider_info);
    updateGPSCoordinates();
locationManager.removeUpdates(GPSTracker.this);

相关文章