i got the latitude and longitude use Location.
and save in
private double mLatitude;
private double mLongitude;.
mLatitude = location.getLatitude();
mLongitude =location.getLongitude();
locationMg =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String bestProvider = locationMg.getBestProvider(criteria, true);
Location location = locationMg.getLastKnownLocation(bestProvider);
if(location != null){
Toast.makeText(this,"Latitude" +location.getLatitude()+"\n"+"Longitude"+location.getLongitude(), 0).show();
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}
and
private String url ="http://api.openweathermap.org/data/2.5/weather?lat="+mLatitude+"&lon="+mLongitude+"&mode=xml";
I saw LogCat After running this code .
However, the value of the longitude and latitude of 0.0 came out.
Why is it?
Is it because the string type url??????????????
and Latitude and longitude should only type is double.
Do change the url?
help.
I do not know what to do.
From the documention
If the provider is currently disabled, null is returned from getLastKnownLocation()
check whether location object is null.
Register for location updates using requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent)
Maybe you are not defining url after location is known. Put ir here:
if(location != null){
Toast.makeText(this,"Latitude" +location.getLatitude()+"\n"+"Longitude"+location.getLongitude(), 0).show();
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
url ="http://api.openweathermap.org/data/2.5/weather?lat="+mLatitude+"&lon="+mLongitude+"&mode=xml";
}
Related
I am trying to store my location into the parse.com database, but i have having an issue doing so. First of all, I can't store anything into the database I believe it is returning null or something so. Not quite sure whats the behavior. Can someone guide me in the right direction?
double longitude = location.getLongitude();
double latitude = location.getLatitude();
// Get longitude of the current location
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
ParseGeoPoint point = new ParseGeoPoint(latLng); //<----- This is where the error is.
parkingobject.put("Location", point);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
ParseGeoPoint geoPoint = new ParseGeoPoint(latitude , longitude );
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
parkingobject.put("Location", geoPoint );
Try this code.
I am working on application where i get MyLocation by Latitude and Longitude .Now I want to get the Users who is in 5 Km within my range .I am using following code to get the Lat and Long of User.
if(gps.canGetLocation()){
getlatitude = gps.getLatitude();
getlongitude = gps.getLongitude();
latitude_mString = String.valueOf(getlatitude);
longitude_mString = String.valueOf(getlongitude);
Log.d("Value of lat and long", latitude_mString+""+longitude_mString);
Log.d("u r", "ur in register lati loni");
new Thread(null,latlongThread,"").start();
}
}else{
gps.showSettingsAlert();
}
marker = new MarkerOptions().position(new LatLng(getlatitude, getlongitude)).title("Me");
//marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.));
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(getlatitude, getlongitude))
.zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.addMarker(marker);
return rootView;
}
By This Code i can get mylocation .I am storing each user data in my API .So i have all data regarding lat and long of each user .So now i want to print user who have location or lat and long within 5 km of me.
Use the location's distanceTo method
http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)
I use the following method to get the current latitude & longitude:
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager == null) {
Toast.makeText(LocationsListActivity.this,
"Location Manager Not Available", Toast.LENGTH_SHORT)
.show();
}
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
but the problem the lat and lng I got only have 8 numbers behind the decimal (lat: 52.51491445, lng: 13.39044515)
What I wanna have is the lat and lng I got has 13 numbers behind the decimal.
How can I do that?
Thanks
You could look into the setAccurary() method and the setPowerRequirement() method of Criteria.
I'm trying to get the latitude and longitude information from an android phone through GPS, when i'm outdoor or under the sky directly i'm able to get the values instantly but when i'm indoor or inside a room its taking more than a minute to get the values. Can anyone help me in getting this values fastly when I'm using my app inside a room.
I'm using the following code in getting the values:
LocationManager locManager;
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
and
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
EditText myLocationText = (EditText)findViewById(R.id.editText1);
EditText myLocationText1 = (EditText)findViewById(R.id.editText2);
String latString = "";
String LongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latString = "" + lat;
LongString ="" + lng;
} else {
latString = "No location found";
LongString = "No location found";
}
myLocationText.setText(""+ latString);
myLocationText1.setText(""+ LongString);
}
Is there any other way in getting the GPS values other than using LocationManager??
You can get the last known location, and it's quite fast:
/**
* Gets the last known location.
*
* #param locationManager the location manager
* #return the last known location
*/
public static Location getLastKnownLocation(LocationManager locationManager)
{
Location bestResult = null;
float bestAccuracy = 10000;
long bestTime = 0;
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Log.d("LOCATION", "Provider: " + provider);
Location location = locationManager.getLastKnownLocation(provider);
Log.d("LOCATION", "Location found? "+ (location==null?"NO":"YES"));
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
Log.d("LOCATION", "Acc: "+ String.valueOf(accuracy) + " -- Time: " + String.valueOf(time));
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
Log.d("LOCATION", "BEST FOUND? "+ (bestResult==null?"NO":"YES"));
return bestResult;
}
You can get the last known location of the device using the following method in LocationManager, this is the location that has been last found system-wide.
This method is the key:
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
See for more details Obtaining User Location
If you are indoors, you would be better of using LocationManager.NETWORK_PROVIDER as you are unlikely to get a signal inside. You could also use getLastKnownLocation(), but this may be non existent or out of date.
If you want get position in a room, gps provider is slowly. You can try changing:
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
By:
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000L,500.0f,
locationListener);
And you will receive location in 'public void onLocationChanged(Location location)' method.
I used Network provider to fetch the Co-Ordinates and it is always much faster inside the house in living room. But when I try the same in the Basement area, it doesn't fetch the coordinates at all though I waited almost couple of minutes.
Note: I could able to make a call from basement area and I am also able to browse as well..
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Just after that i take the latitude and the longitude to put in on a server, but it crash my phone.
Settings>Location and Security>My
Location
service is not enabled into your Android device
hence yourlocationManager.getLastKnownLocation(provider) ( If the provider is currently disabled, null is returned.) is producing null value, Later then you are invoking double latitude = location.getLatitude(); double longitude = location.getLongitude(); with a null value object, so your application is crashing.