private Location copyOf(Location location) { if (location == null) return null; Location copy = new Location(location); copy.setAccuracy(location.getAccuracy()); copy.setAltitude(location.getAltitude()); copy.setBearing(location.getBearing()); copy.setExtras(location.getExtras()); copy.setLatitude(location.getLatitude()); copy.setLongitude(location.getLongitude()); copy.setProvider(location.getProvider()); copy.setSpeed(location.getSpeed()); copy.setTime(location.getTime()); return copy; }
public TrackLocation(){ isRunning=true; location=new Location("not defined"); location.setLatitude(0); location.setLongitude(0); } @Override
@Override public void onLocationChanged(Location location) { XLog.d("Location: %f/%f", location.getLatitude(), location.getLongitude()); }
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); return true; LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { timer1.cancel(); locationResult.gotLocation(location); lm.removeUpdates(this); lm.removeUpdates(locationListenerNetwork); }; if(gps_loc.getTime()>net_loc.getTime()) locationResult.gotLocation(gps_loc); else locationResult.gotLocation(net_loc);
locationManager.removeUpdates(locationListenerGps); locationManager.removeUpdates(locationListenerNetwork); gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (networkEnabled) networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (gpsLocation.getTime() > networkLocation.getTime()) locationResult.gotLocation(gpsLocation); else private final LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { timer.cancel(); locationResult.gotLocation(location); locationManager.removeUpdates(this); locationResult = result; if (locationManager == null) locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); timer = new Timer(); timer.schedule(new GetLastLocationTask(), maxMillisToWait); return true;
MyLocation.lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if(network_enabled) MyLocation.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); timer1=new Timer(); timer1.scheduleAtFixedRate(new GetLastLocation(), 0,50000); return true; LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { timer1.cancel(); locationResult.gotLocation(location); MyLocation.lm.removeUpdates(this); MyLocation.lm.removeUpdates(locationListenerNetwork); }; if(gps_loc.getTime()>net_loc.getTime()) locationResult.gotLocation(gps_loc); else locationResult.gotLocation(net_loc); timer1.cancel(); lm.removeUpdates(locationListenerGps); lm.removeUpdates(locationListenerNetwork);
private static Intent _intent; private static LocationManager locationManager; private static LocationListener locationListener = new LocationListener() { _intent = intent; locationManager = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); if (locationManager.isProviderEnabled(provider)) { locationManager.requestLocationUpdates(provider, MIN_TIME_REQUEST, 5, locationListener); Location gotLoc = locationManager .getLastKnownLocation(provider); gotLocation(gotLoc); } else { android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); settinsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); _context.startActivity(settinsIntent); } else if (prevLocation == null) { return true; } else if (currentLocation.getTime() == prevLocation.getTime()) { return false; } else {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); for (final String provider : locationManager.getAllProviders()) { final Location location = locationManager.getLastKnownLocation(provider); final long now = System.currentTimeMillis(); if (location != null && (bestLocation == null || location.getTime() > bestLocation.getTime()) && location.getTime() > now - MAX_AGE) { bestLocation = location; locationManager.requestSingleUpdate(criteria, pendingIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); final String position = bestLocation.getLatitude() + "," + bestLocation.getLongitude(); final Intent mapIntent = getMapIntent(position); .setContentText(context.getString(R.string.display_last_position)) .setContentTitle(context.getString(R.string.app_name)) .setSmallIcon(R.drawable.ic_launcher)
@Override public void loadLocation(Context context, final LoadLocationListener listener) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { LogUtils.e(TAG, "location failure."); listener.onFailure("location failure.", null); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(location == null) { LogUtils.e(TAG, "location failure."); return; double latitude = location.getLatitude(); //经度 double longitude = location.getLongitude(); //纬度 String url = getLocationURL(latitude, longitude); OkHttpUtils.ResultCallback<String> callback = new OkHttpUtils.ResultCallback<String>() {
public RouteByAddressCallbacks(final Context context, final AlertDialog.Builder builder, final BoundingBox boundingBox, final Location lastFix, final Waypoints waypoints) { context_ = context; START_MARKER_LABEL = context_.getResources().getString(R.string.rba_start); FINISH_MARKER_LABEL = context_.getResources().getString(R.string.rba_finish); WAYPOINT_LABEL = context_.getResources().getString(R.string.rba_waypoint); final View layout = View.inflate(context, R.layout.routebyaddress, null); builder.setView(layout); builder.setPositiveButton(R.string.find_route, MessageBox.NoAction); bounds_ = boundingBox; currentLoc_ = lastFix != null ? new GeoPoint(lastFix.getLatitude(), lastFix.getLongitude()) : null; placeHolder_ = layout.findViewById(R.id.places); waypoints_ = waypoints; addWaypoint_ = layout.findViewById(R.id.addVia); addWaypoint_.setOnClickListener(this); routeType_ = layout.findViewById(R.id.routeType); final View from = addWaypointBox(); addWaypointBox(); if (currentLoc_ == null) from.requestFocus(); }
public boolean getMyLocation(final Context context, final LocationView view) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { return false; Location curLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (null == curLoc) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, new LocationListener() { @Override public void onLocationChanged(Location location) { view.onLocationChanged(-1, 0, 0, strAddr); } else { view.onLocationChanged(0, curLoc.getLatitude(), curLoc.getLongitude(), strAddr);
void pushLocation(double lat, double lon,double alt,float accuracy) { LocationManager lm = (LocationManager) ctx.getSystemService( Context.LOCATION_SERVICE); Location mockLocation = new Location(providerName); mockLocation.setLatitude(lat); mockLocation.setLongitude(lon); mockLocation.setAltitude(alt); mockLocation.setTime(System.currentTimeMillis()); mockLocation.setAccuracy(accuracy); if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); } lm.setTestProviderLocation(providerName, mockLocation); }
public static Location getLocation2(Context cxt) { LocationManager mgr = (LocationManager) cxt.getSystemService(Context.LOCATION_SERVICE); List<String> providers = mgr.getProviders(true); Location last = null; for (Iterator<String> i = providers.iterator(); i.hasNext(); ) { Location loc = mgr.getLastKnownLocation(i.next()); // If this provider has a last location, and either: // 1. We don't have a last location, // 2. Our last location is older than this location. if (loc != null && (last == null || loc.getTime() > last.getTime())) { last = loc; } } return last; }
context.getSystemService(Context.LOCATION_SERVICE); if(locationManager == null) return null; Location lastGpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location lastNetworkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); Location bestCachedLocation = null; if(bestCachedLocation == null || (System.currentTimeMillis() - bestCachedLocation.getTime() > 30E+3))
gpsTimer = new Timer(); long checkInterval = getGPSCheckMilliSecsFromPrefs(); long minDistance = getMinDistanceFromPrefs(); for (String s : locationManager.getAllProviders()) { locationManager.requestLocationUpdates(s, checkInterval, minDistance, new LocationListener() { float distance = l.distanceTo(lastLocation); Log.d(TAG, "Distance to last: " + distance); if (l.distanceTo(lastLocation) < minDistance && !force) { Log.d(TAG, "Position didn't change"); return; if (l.getAccuracy() >= lastLocation.getAccuracy() && l.distanceTo(lastLocation) < l.getAccuracy() && !force) { Log.d(TAG, "Accuracy got worse and we are still " return; if (l.getTime() <= lastprovidertimestamp && !force) { Log.d(TAG, "Timestamp not never than last"); return;
@Override public void execute(Context context, CommandInstance commandInstance, CommandExecResult result) throws Exception { Location location = LocationUtils.GetLocation(context, 20000); if (location == null) throw new Exception("Location Request timed out"); String locationDescription = String.format(Locale.ENGLISH, "%1$.4f %2$.4f", location.getLatitude(), location.getLongitude()); String mapsLinkParam = String.format(Locale.ENGLISH, "%1$.4f,%2$.4f", location.getLatitude(), location.getLongitude()); String mapsLink = "https://www.google.com/maps?q=" + mapsLinkParam; // get timestamp in RFC3339 format String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ") .format(new Date(location.getTime())); String accuracy = location.hasAccuracy() ? String.format(Locale.getDefault(), "%1$.0fm radius (68%% probability)", location.getAccuracy()) : context.getString(R.string.unknown_accuracy); // see https://developer.android.com/reference/android/location/Location.html#getAccuracy() result.setCustomResultMessage(context.getString( R.string.result_msg_location_coordinates, locationDescription, timestamp, accuracy, mapsLink)); result.setForceSendingResultSmsMessage(true); } }
String myName = me == null ? "" : Util.getDisplayName(me); JSONObject o = new JSONObject() .put(LocationCellFactory.KEY_LATITUDE, location.getLatitude()) .put(LocationCellFactory.KEY_LONGITUDE, location.getLongitude()) .put(LocationCellFactory.KEY_LABEL, myName); String notification = context.getString(R.string.atlas_notification_location, myName); MessagePart part = client.newMessagePart(LocationCellFactory.MIME_TYPE, o.toString().getBytes()); PushNotificationPayload payload = new PushNotificationPayload.Builder()
private void openMaps(Context context) { try { if (location != null) { final String uri = "geo:" + location.getLatitude() + "," + location.getLongitude(); final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { Toast.makeText(context, R.string.dd_debug_drawer_location_not_found, Toast.LENGTH_SHORT).show(); } } catch (ActivityNotFoundException e) { Toast.makeText(context, R.string.dd_debug_drawer_location_map_not_found, Toast.LENGTH_SHORT).show(); } }
public Location getLastLocation() { // Default is middle of USA, Kansas City float lon = mPref.getFloat(mContext.getString(R.string.GPS) + "lon", -94.5f); float lat = mPref.getFloat(mContext.getString(R.string.GPS) + "lat", 39.5f); Location l = new Location(LocationManager.GPS_PROVIDER); l.setLongitude(lon); l.setLatitude(lat); return l; }
private void updateLocation(Location location) { if (location != null) { this.location = location; latitude.setText(String.valueOf(location.getLatitude())); longitude.setText(String.valueOf(location.getLongitude())); accuracy.setText(String.valueOf(location.getAccuracy()) + "m"); final Date date = new Date(location.getTime()); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); time.setText(sdf.format(date)); provider.setText(location.getProvider()); } else { latitude.setText(R.string.dd_debug_drawer_location_empty); longitude.setText(R.string.dd_debug_drawer_location_empty); accuracy.setText(R.string.dd_debug_drawer_location_empty); time.setText(R.string.dd_debug_drawer_location_empty); provider.setText(R.string.dd_debug_drawer_location_no_provider); } }