My app gets updated GPS coordinates periodically which I show using a Marker on map. I need to move the marker to a new position if the new GPS coordinates are different then what Marker is currently showing.
The problem is that comparing Marker.getPosition() is more accurate while LatLang is not, hence sometimes even when they are the same, my logic says they are different.
How to solve this issue?
Please note that the same LatLang i assign to Marker.
You can consider that two LatLngs are virtually the same if the distance between them is less than a given tolerance.
You can use the SphericalUtil.computeDistanceBetween method from the Google Maps Android API Utility Library
float YOUR_TOLERANCE = 1; // 1 meter
if (SphericalUtil.computeDistanceBetween(pos, buslatLng) < YOUR_TOLERANCE) {
// Both locations are considered the same
}
Related
I try to get the distance between two points by showing them together with a polyline. One marker is permanently somewhere and the other the user chooses the place they want when clicking on the map. I tried with many codes obtained from the Internet but for me neither works or just gives me the marker of a place with the latutude and longitude. I want to use retrofit 2 to bring the information. Can anyone help me with that?
On clicking on map you are placing marker, So if you want to get location of that marker then simply you can got that marker position using that marker object like this.
Double latitude = marker.getPosition().latitude;
Double longitude = marker.getPosition.longitude;
and you have already one location so you can find distance between two locations using this.
locationFrom.distanceTo(locationDestination);
here locationFrom is your source location from where you want to find distance and locationDestination is your destination location to where you find distance.
What would be the best way to know the nearest place of the list against the current user location.
is there a way to calculate the difference between two geo points in maps?
You can get the distance between 2 points making 2 Location objects (one for each point) and calling the method distanceTo.
Example.
// Instantiate.
Location pointOne=new Location("");
Location pointTwo=new Location("");
// Configure.
pointOne.setLatitude(doubleValue);
pointOne.setLongitude(doubleValue);
pointTwo.setLatitude(doubleValue);
pointTwo.setLongitude(doubleValue);
// Calculate distance.
float distanceInMeters=pointOne.distanceTo(pointTwo);
And that's all.
By the way, watch your grammar for better readibility =)
Good luck.
I have a problem with lonitude and latitude in google maps.
This is way how I add my markers:
Marker newStore = map.addMarker(new MarkerOptions().position(new LatLng(store.getLatitude(), store.getLongitude()))
.title(store.getName()).icon(BitmapDescriptorFactory.defaultMarker(markerResolver("up"))));
where store.getLatitude = 44.04687 and store.getLongitude = -70.295734.
When I print marker position after add them this is what I got:
marker.getPosition().latitude = 44.046873756 and marker.getPosition().longitude = -70.295734543234.
Now I want to equal both data to search my store by position, but I can't do that, because store position is different then marker position. Markers position is more precise then sote position. How can I avoid that. I ask that because I have a list of stores and add markers by position. Now I want to get which store I click by position but as you can see position of marker and sotre is different.
You should not compare Markers or any other objects by LatLng position, because of this bug.
You may instead keep a Map<Marker, Store> with all your Markers and Stores and retrieve it like this in onMarkerClick:
Store store = map.get(marker);
More on that and other approaches here: https://stackoverflow.com/a/17000070/2183804
One option is to leverage the concept of Geohashing to evaluate equality based on proximity between two points. Geohashes sizes (distance) can be configured upon instantiation. So in your example you could construct to Geohashes like so:
GeoHash myLocHash = GeoHash.withCharacterPrecision(store.getLatitude(), store.getLongitude(), 7);
GeoHash markerLocHash = GeoHash.withCharacterPrecision(marker.getPosition().latitude, marker.getPosition().longitude, 7);
if (myLocHash.equals(markerLocHash)) {
// Same location +/- .61 km margin of error
}
This is probably overkill for what you need, but can be a really handy option for other applications. Here is a great Java library for Geohashes:
https://github.com/kungfoo/geohash-java
More reading on Geohashes:
http://en.wikipedia.org/wiki/Geohash
How to find distance from polyline in google map in android using map apiV2 ? I want to find distance when user start moving from one location to another .
I used the Calculate distance in meters when you know longitude and latitude in java but it takes distance between two lat-long not as per user
moves in map. Please help me if anyone knows how to find distance using polyline.
For finding distance from your polyline you can use PolyUtil class which contains one method distanceToLine .
implementation'com.google.maps.android:android-maps-utils:0.5+'
add this library in your build.gradle.
For more you can use Google map utility class
https://developers.google.com/maps/documentation/android-api/utility/
For this you need to use thread. In that at regular interval you should find the distance between current coordinates and last saved coordinate.
Raw code:
Coordinate current, last;
Distance D = 0;
while (true) {
wait(10000);
last = current;
current = getNewCordinate;
d = D + distFrom(current, last);
}
This logic will help.
Based on the answer given by Gevaria Purva I found a more appropriate method on PolyUtil named isLocationOnPath.
For the third parameter most likely you want to pass true.
I am developing an application that shows the distance between the user and multiple locations (such as foursquare in the image below) on android, I am using eclipse and would like to know how to calculate the distance between various points. Thank you!
Image:
http://i.stack.imgur.com/rsWmO.jpg
There are probably many ways to get this done, here's an option.
You could use the distanceTo() method. If you want more than one distance, simply use a loop to repeat it until you've calculated the distances between all the Locations you have at hand.
If you're using Google Maps, you can use a Distance Matrix
https://developers.google.com/maps/documentation/distancematrix/
https://developers.google.com/maps/documentation/javascript/distancematrix
Here I am providing you some sample code for Distance calculation. I have done like this in my project. Here distanceTo() method will return you the distance in double.
private Location currentLocation, distanceLocation;
double distance = 0;
//set your current location
currentLocation.setLatitude(currentLat);
currentLocation.setLongitude(currentLong);
//set your destination location
distanceLocation = new Location("");
distanceLocation.setLatitude(destinatioLat);
distanceLocation.setLongitude(destinationLong);
distance = currentLocation.distanceTo(distanceLocation)/1000;
Same for more then one location you can use Array for storing distance. Its on you how you want to use it as per your requirement.
Hope this will help you.