in my android app i calculate gps position and show the lat log value into TextView, and i use this lat long value for a query on SQLITE DB.
Now i'm trying to show into TextView the name of Location calculated by lat and log. I have try geocodeby i have soma error.
this is my code for calculate the lat and long value:
ImageView bt_get_mygpspos = (ImageView)getView().findViewById(R.id.bt_get_mygpspos);
edittext_mygpspos = (EditText)getView().findViewById(R.id.edittext_mygpspos);
bt_get_mygpspos.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
GPSTracker mGPS = new GPSTracker(getActivity());
if(mGPS.canGetLocation() ){
query.curPos = new LatLng(mGPS.getLatitude(),mGPS.getLongitude());
edittext_mygpspos.setText("lat:"+query.curPos.latitude+" lon:"+query.curPos.longitude);
Log.e("test", "lat:"+query.curPos.latitude+" lon:"+query.curPos.longitude);
}else{
mGPS.showSettingsAlert();
}
}
});
how i can trasform the mGPS.Latitude and Longitude in a name of Location to show into TextView?
Thans
You could use any public web-based API for geocoding like:
Google API;
Yandex API;
Yandex is better in ex-USSR space and have better daily free limits: 25 000 request when compare to 2 500 in Google. In any other detail Google is better.
Related
Sorry for not providing any sample code because I don't have any idea about how to do this.
I'm coding an Android application which uses Firebase as the backend.
I'm storing the current current user location to mysl db using the fusedlocationapi.
Now, when a user is logged into the application, he/she is given a option to show only the list of user around a radius of 25km (let's assume as an example). Can any one guide me with this feature please.
I don.t wanna do this on client side ...is there any way to so it using mysql
You can use Google Maps Android API utility library
public class DistanceHelper {
private static final Double RADIUS = 25000.0;
public static double getDistance(LatLng user1, LatLng user2){
double distance = SphericalUtil.computeDistanceBetween(user1, user2);
return distance;
}
public static Double getRadius(){
return RADIUS;
}
public static String formatNumber(double distance) {
String unit = "m";
if (distance < 1) {
distance *= 1000;
unit = "mm";
} else if (distance > 1000) {
distance /= 1000;
unit = "km";
}
return String.format("%4.3f%s", distance, unit);
}
}
LatLng user1 = new LatLng(currentUser.getLatitude(), currentUser.getLongitude());
LatLng user2 = new LatLng(otherUser.getLatitude(), otherUser.getLongitude());
Double distance = DistanceHelper.getDistance(user1, user2);
if (distance <= DistanceHelper.getRadius()) {
//code here
}
I store the users' latitude and longitude in FB database using GPS and LocationManager. put the calculation inside your ChildEventListener to retrieve get users inside 25km radius.
However I think this implementation is wrong because the calculations are done on client side so what if you have million or billion of users then you have to compute each one by one on client's device which might cause very slow performance of your app.
I have been using https://github.com/pires/android-obd-reader to use OBD data capture in realtime on the obd activity.
Basically, the information is updated every few seconds on this activity.
All I want is to use this information (fuel level for example) in real time with another activity and update the text not once but in real time.
public void onLocationChanged(Location location){
latitude = location.getLatitude();
longitude = location.getLongitude();
drawMyTrack(latitude, longitude, prevLatitude, prevLongitude);
prevLatitude = latitude;
prevLongitude = longitude;
sendLocationDataToWebsite(location);
text= currentTrip.getFuelLevel();
TextView txtChanged = (TextView)findViewById(R.id.textView2);
txtChanged.setText(text);
}
I'm wrinting a application and I have to show the distance covered while I'm running.
I use the function "public void onLocationChanged" of the LocationListener. When the user tap a botton and start running I want to show the distance he covered updated to the point in which he is located.
I've written this code:
public void onLocationChanged(Location location) {
if(location != null) {
if(location.hasSpeed()){
if(latitude1 == 0 && longitude1 == 0){
latitude1 = (location.getLatitude()*Math.PI)/180;
longitude1 = (location.getLongitude()*Math.PI)/180;
} else{
latitude2 = (location.getLatitude()*Math.PI)/180;
longitude2 = (location.getLongitude()*Math.PI)/180;
distance = (6372.795477598)*Math.acos(Math.sin(latitude1)
*Math.sin(latitude2)+Math.cos(latitude1)
*Math.cos(latitude2)*Math.cos(longitude1-longitude2));
sumDistance += distance;
latitude1 = latitude2;
longitude1 = longitude2;
}
tv.setText("Distance covered=" + sumDistance + " m");
}
}
}
Is it accurated?
Just a Suggestion:
Store the Latitude and Longitude of the start location and the end location when the user clicks the appropriate button.
and then you could use distanceBetween or distanceTo to get the distance between those two geoPoints.
P.S: This may not work if the user will start and end his run at the same point ;)
Addition:
Check this tutorial:
Recently google has improved its location based API's. They have fused the sensors with the location based api's to share examples of how location can be made more accurate (about 4 times more effective) and consume much lesser power (10 times lesser).
Some interesting links for you to go through will be this and video google io.
SO link for how to use the API's here
I have given coordinates for latitude and longitude, and I want make a location object with those.There isn't a constructor that takes to doubles to make a location, so I tried like this:
Location l = null;
l.setLatitude(lat);
l.setLongitude(lon);
but it crashes. Any different idea?
Just create a new Location with new Location("reverseGeocoded");
like this
GeoPoint newCurrent = new GeoPoint(59529200, 18071400);
Location current = new Location("reverseGeocoded");
current.setLatitude(newCurrent.getLatitudeE6() / 1e6);
current.setLongitude(newCurrent.getLongitudeE6() / 1e6);
current.setAccuracy(3333);
current.setBearing(333);
This crashes because you cannot call methods on an non existent object.
I presume you are talking about android.location.Location?
This is usually returned by the various positioning services of Android.
What do you want to do with it?
Do you want to reverse geocode it? As in find an address for that geo coordinate?
Or do you want to use it as a "fake" position and feed it to other applications?
There are BTW two constructors. One takes the name of a positioning service and the other is a copy constructor and takes an existing Location.
So you could create a Location like this:
Location l = new Location("network");
But I do not think that this will result in something you want to have.
Here is a link to the documentation:
https://developer.android.com/reference/android/location/Location.html#Location%28java.lang.String%29
Location l = new Location("any string");
l.setLatitude(lat);
l.setLongitude(lon);
Try this:
public double lat;
public double lon;
public void onLocationChanged(Location loc)
{
lat=loc.getLatitude();
lon=loc.getLongitude();
Toast.makeText(getBaseContext(),"Latitude:" + lat +Longitude"+lon,Toast.LENGTH_LONG).show();
}
I am developing an app where I need to calculate the distance from the current position and some other locations. I am using the GPS to access the users current location and the other locations coordinates are stored in a database. The problem occurs in the following snippet:
#Override
public void onLocationChanged(Location arg0) {
Log.v("LOCATION LAT", String.valueOf(arg0.getLatitude()));
currentLocation = arg0; //currentLocation is a global class variable
}
The problem is when I feed the DDMS with coordinates such as:
Latitude: 62.639579
Longitude: 17.909689 and log these values I get Latitude: 62.0 and Longitude 17.0 .
If I create a location object and set the lat and lng values myself it works. Like this:
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current.setLatitude(62.639579);
current.setLongitude(17.909689);
Log.v("Current LAT", "" + current.getLatitude());
}
EDIT SOLVED:
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently this should be delimited with a comma sign, not a dot...
Have you used the permissions specified in this post? Else it kicks back to using cell tower triangulation.
Other question
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently the coordinates should be delimited with a comma sign, not a dot...
you can do something like as below. Create a location variable in that you have to assign location change var
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current=arg0;
Log.v("Current LAT", "" + current.getLatitude());
}