The last know location could be null. So you need a further check for this, then ask for a quick update:
if(location == null){
String provider = locationManager.getBestProvider(criteria, true);
if (provider != null){
locationManager.requestLocationUpdates(provider, 0, 0, singeUpdateListener, context.getMainLooper());
}else {
Log.e("TAG", No location set");
}
}
class variable:
protected LocationListener singeUpdateListener = new LocationListener() {
public void onLocationChanged(Location location) {
String lati = Double.toString(location.getLatitude());
String longi = Double.toString(location.getLongitude());
locationManager.removeUpdates(singeUpdateListener);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
Answer from Blundell on Stack ExchangeFirst you need to define a LocationListener to handle location changes.
int LOCATION_REFRESH_TIME = 15000; // 15 seconds to update
int LOCATION_REFRESH_DISTANCE = 500; // 500 meters to update
....
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//your code here
}
};
Then get the LocationManager and ask for location updates
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);
}
And finally make sure that you have added the permission on the Manifest,
For using only network based location use this one
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
For GPS based location, this one
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
You need to write code in the OnLocationChanged method, because this method is called when the location has changed. I.e. you need to save the new location to return it if getLocation is called.
If you don't use the onLocationChanged it always will be the old location.
Videos
There is one other way to achieve it using java only.
You can use selenium+PhantomJS to hit "https://mycurrentlocation.net/". This website by default will load you current location and you can read the data on this website using Selenium API.
PhantomJS is an invisible browser, it can open any website in invisible mode and read data from the same.
Hope this helps.
There is not an API for location within Google+. You can use the Google Maps Places API to get information of interest located close to a user. Before you can make API queries to the Google Maps Places API, you will need geolocation data (latitude and longitude) to make your queries.
For getting GPS data from the user's laptop, there are a number of approaches, however, I personally prefer to use a location database and perform lookups on the user's IP. The GeoLite databases from Maxmind are really easy to use: you get the user's IP address and then query the database for the location (specificity will vary based on the database used) and the location data will include anything you need for mapping purposes. This stack overflow question on geolocation includes a link to some example code that should get you bootstrapped.
You may also want to consider the geolocation API for modern browsers if you're using a web integration on the desktop.
Once you have lat/long coordinates, you can get location data using the Places API by passing in the coordinates to Place search APIs.
If you want to get location data from Android, you should use a location provider to set up location status updates. The Android documentation does a better job explaining the steps than I can here.
To transmit the data to your computer, you can use bluetooth.
Create a data member of Location class.
public static Location mLocation;
use OnMyLocationChangeListener on Map
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
mLocation = location;
displayCurrentLocation();
}
});
public void displayCurrentLocation(){
CameraPosition cameraPosition = new CameraPosition.Builder().
target(new LatLng(mLocation.getLatitude(), mLocation.getLongitude())).
tilt(60).
zoom(15).
bearing(0).
build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
hope it will works.
You can achieve this in two ways
- Run a service (background operation) and get current latitude and longitude at the app starting itself , before coming to the targeted screen
- Expose methods in service which you can access from Activity
- Show users current location from the service method input
- Or else implement a listener which will every time update the users current location
You can refer the following Example 1 and Example 2
getSystemService() is a method of the Context class. Your class does not subclass Context. Generally you would use getSystemService() in an Activity (which is a subclass of Context).
I think your referring to a compiler error saying something like "method getSystemService not found". The getSystemService is held with the Context class which is obtained in your Application. Check out this post to see how to get Context.
Static way to get 'Context' on Android?
The biggest DB with WiFi-to-location is Skyhook wireless. You can get the SDK from here : Skyhook Location SDK
It has not fully coverage, but it's the best in my opinion(Google and Apple use it).
They are maintaining huge IP databases for tracking user geo location based on host.
And as a individual it's not possible to maintain all those host mapping. So for this you have to bye IP table form third party provider.
However if you want, you can get the location details viz city,region,country,longitude,latitude from request header.
It's only available for GAE user(IP table is maintained by google).
If you want you can host a sample application at GAE and use webservice call to get location details(SOA) from your server.Particularly i am talking for non-GAE user.
For more details go through GAE ReleaseNotes
country = request.getHeader("X-AppEngine-Country");
region = request.getHeader("X-AppEngine-Region");
city = request.getHeader("X-AppEngine-City");
temp = request.getHeader("X-AppEngine-CityLatLong");