Unable to get current location Google Map Android - android

I know this question is similar to other question but i am unable to understand the procedure of getting my location in google map. I have tried lots of tutorials but i think i am missing something.
I am using the following code in my main class :
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
//I am getting bestProvider = gps here
String bestProvider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//getting location null here
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}else if((ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)){
//getting location null here
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
}
#Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}

Here you go.You can use this below code sample in your application:
public class CurrentLocation {
Timer timer;
LocationManager locationManager;
LocationResult locationResult;
boolean gpsEnabled=false;
boolean networkEnabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(locationManager ==null)
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gpsEnabled && !networkEnabled)
return false;
if(gpsEnabled)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(networkEnabled)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer =new Timer();
timer.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
locationResult.gotLocation(location);
locationManager.removeUpdates(this);
locationManager.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
locationResult.gotLocation(location);
locationManager.removeUpdates(this);
locationManager.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
#Override
public void run() {
locationManager.removeUpdates(locationListenerGps);
locationManager.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gpsEnabled)
gps_loc= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(networkEnabled)
net_loc= locationManager.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())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
}

Use this code for get current location by GPS provider or network provider
if (locationManager == null) {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled) {
location = getLastLocationByProvider(locationManager, LocationManager.GPS_PROVIDER, getApplicationContext());
} else if (isNetworkProviderEnabled) {
location = getLastLocationByProvider(locationManager, LocationManager.NETWORK_PROVIDER, getApplicationContext());
}
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {
if (isNetworkProviderEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100000, 1, this);
provider_info = LocationManager.NETWORK_PROVIDER;
} else if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100000, 1, this);
provider_info = LocationManager.GPS_PROVIDER;
} else {
alertDialog = Util.showOkDialog(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Env.currentActivity != null) {
if (Env.currentActivity instanceof LocationActivity) {
try {
gotoSettings();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (alertDialog != null) {
alertDialog.dismiss();
alertDialog = null;
}
}
}, this.getResources().getString(R.string.location_service_validation));
}
location = locationManager.getLastKnownLocation(provider_info);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}

Related

Get current location latitude and longitude without using GPS

hi friends i am in trouble i want to find my current location lat long without using GPS ,can anyone help me..
Below is my code but its not working
public Location getLocation() {
Location location = null;
final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
try {
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
//updating when location is changed
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.i("latitudelongitude", longitude + "," + latitude);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isNetworkEnabled && !isGPSEnabled) {
//call getLocation again in onResume method in this case
// startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} else {
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} else {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
//Request for the ACCESS FINE LOCATION
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
//call the getLocation funtion again on permission granted callback
}enter code here
return location;
}

Android location updates Google Maps every second

I want to display location updates every second in android. The problem is that with this code on one mobile phone the location is updated every 30-40 seconds and on another one every 5-6 seconds. How can i solve the problem to update every second? What is wrong in my code? I have the following code
private LocationManager mLocationManager = null;
private String provider = LocationManager.GPS_PROVIDER;
private void locateCurrentPosition() {
int status = getActivity().getPackageManager().checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,
getActivity().getPackageName());
if (status == PackageManager.PERMISSION_GRANTED) {
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (ApplicationState.choice_gps_loc == 0&&location!=null) {
ApplicationState.latitude = String.valueOf(location.getLatitude());
ApplicationState.longitude = String.valueOf(location.getLongitude());
addBoundaryToCurrentPosition(Double.parseDouble(ApplicationState.latitude)
, Double.parseDouble(ApplicationState.longitude), map);
}
Log.e("Location", "nuuuu");
// mLocationManager.addGpsStatusListener(this);
long minTime = 1000;// ms
float minDist = 1.0f;// meter
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
private boolean isProviderAvailable() {
mLocationManager = (LocationManager) getActivity().getSystemService(
Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = mLocationManager.getBestProvider(criteria, true);
if (mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
return true;
}
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
return true;
}
if (provider != null) {
return true;
}
return false;
}
private void updateWithNewLocation(Location location) {
if (location != null && provider != null) {
double lng = location.getLongitude();
double lat = location.getLatitude();
Log.e("alta", lng + " " + lat);
if (ApplicationState.choice_gps_loc == 0)
addBoundaryToCurrentPosition(lat, lng, map);
// CameraPosition camPosition = new CameraPosition.Builder()
// .target(new LatLng(lat, lng)).zoom(10f).build();
if (ApplicationState.choice_gps_loc == 0) {
ApplicationState.latitude = String.valueOf(lat);
ApplicationState.longitude = String.valueOf(lng);
}
if (myMarker != null) myMarker.remove();
myMarker = map.addMarker(new MarkerOptions()
.title("My position/my future destination")
.position(new LatLng(Double.parseDouble(ApplicationState.latitude),
Double.parseDouble(ApplicationState.longitude)))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
//if (map != null)
// map.animateCamera(CameraUpdateFactory
// .newCameraPosition(camPosition));
} else {
Log.d("Location error", "Something went wrong");
}
}
public void addBoundaryToCurrentPosition(double lat, double lang, GoogleMap map1) {
if (updatecerc == 0) {
if (mOptions != null) mOptions.visible(false);
mOptions = new CircleOptions()
.center(new LatLng(lat, lang)).radius(Integer.parseInt(ApplicationState.radius) * 1000)
.strokeColor(0x110000FF).strokeWidth(1).fillColor(0x110000FF);
map1.addCircle(mOptions);
updatecerc++;
}
}
#Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
break;
case LocationProvider.AVAILABLE:
break;
}
}

Fetching latitude and longitude programmaticaly in android always getting GPS LastKnownLocation null

Hello all i know this question is too old and there are many of them but none of the solution is working for me. I am fetching latitude and longitude programmaticaly i tried below code but what is happening is for GPS every time i am getting LastKnownLocation = null also my GPS is enabled on my device, i tried this code on different devices where on some of them i am able to get latitude and longitude from GPS but for most of them it is showing null. I don't know why this code is failing for most of the cases, if any one of you know anything about this or any better way of doing it then please tell me i have spent almost a week digging what's wrong in the above code but nothing help me out.
UPDATE- Is google's FusedLocationApi free or there is limited requests per day ??
public class GPSTracker extends Service implements LocationListener {
private final Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isGPSEnabled) {
Log.d("gps", "gpsenabled");
if (location == null) {
Log.d("gps", "location is null");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
Log.d("gps", "locationManager is not null");
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d("gps", "location is not null");
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("gps", "lat: " + latitude + ", lon: " + longitude);
}
}
}
}
if (isNetworkEnabled) {
Log.d("gps", "networkenabled");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
Log.d("gps", "locationManager is not null");
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.d("gps", "location is not null");
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("gps", "lat: " + latitude + ", lon: " + longitude);
}
}
}
//
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = lm.getBestProvider(criteria, false);
Log.d("gps", "bestProvider: " + bestProvider);
Location location = lm.getLastKnownLocation(bestProvider);
Log.d("gps", "lat: " + location.getLatitude() + ", lon: " + location.getLongitude());
//
}
} catch (Exception e) {
}
return location;
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
latitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to setttings menu ?");
alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
#Override
public void onLocationChanged(Location location) {
this.location = location;
latitude = getLatitude();
longitude = getLongitude();
Log.d("gps", "onLocationChanged");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}

How to get devices coordinate the correct, fast and accurate way?

A year ago I've searched how to get the devices lat,long. I found out in an article here that the correct way is for my activity to implement LocationListener and I've used the code that was provided to achieve it and it worked well.
public Location getLocation() {
LocationManager mLocationManager;
Location mLocation = null;
try {
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
enableLocationProvider();
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 20000, 1, this);
Log.d("Network", "Network");
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
double lat = mLocation.getLatitude();
double lng = mLocation.getLongitude();
}
}
}
//get the location by gps
if (isGPSEnabled) {
if (mLocation == null) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000,1, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation != null) {
double lat = mLocation.getLatitude();
double lng = mLocation.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
I've tried to do this now again and used a phone that hasn't been used for a while and I could get the location. By the way when I opened
Google maps
After a few seconda the app located my coordinates. I'm sure that I'm doing something wrong. How can I locate the device's coordinates as efficiently as google maps does it?
The challenge is GPS takes some time to initialize and meanwhile if you try to access the location it will give you a null. So far this is the best approach I've found in Stack Overflow. Its robust and avoid getting a null and ensures a location data in return whenever the GPS is finished initializing
MyLocation class for getting location changes,
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
public boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
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);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
#Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.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())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}
To receive the location updates override the gotLocation() method which will give you a data once there's a location change.
MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
#Override
public void gotLocation(Location location) {
//Got the location!
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(getActivity(), locationResult);

location with internet service provider(network provider)

Hello I have class that looks up for your location, first with network provider, then waits for GPS provider info. I don't know why, but I never get information from network provider, GPS works well, when signal is acquired.
public class GpsListener {
public static GpsListener refrence = null ;
public LocationManager locationManager = null;
public LocationListener locationListener = null;
public Location location = null;
public static GpsListener getInstance(){
if(refrence == null){
refrence = new GpsListener();
}
return refrence;
}
public void startGpsCallBack(Context activityContext){
locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE);
locationListener = new mylocationlistener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Map.MyPositionlatitude = location.getLatitude();
Map.MyPositionlongitude = location.getLongitude();
}
}
public class mylocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Map.MyPositionlatitude = location.getLatitude();
Map.MyPositionlongitude = location.getLongitude();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public void stopGpsCallBack(){
if (locationManager != null) {
locationManager.removeUpdates(locationListener);
}
}
public void startGpsCallbackAgain(Context activityContext){
locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE);
locationListener = new mylocationlistener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Map.MyPositionlatitude = location.getLatitude();
Map.MyPositionlongitude = location.getLongitude();
}
}
}
use google play serivces it is more simple

Categories

Resources