I am trying to develop an application using MKMapView which needs to plot shortest path.Following is the scenario.
I have 5 to 10 coordinates,which are plotted on the map.I have to draw the shortest path to cover all these locations from my current location.This is a travel app,so need to show optimum distance .Please share your thoughts.I had a thought of finding distance to each point from current location but this needs to send multiple request at a time ,which seems to be very hectic process because the point can be 10 to 100.
Is there any solution for this available in Google Maps SDK?
Looking forward for great ideas and suggestions.
Thanks in advance...
You can use the google maps directions api (https://developers.google.com/maps/documentation/directions/#JSON) to find the shortest path. Then you have two options:
Draw it in a GMSMapView
Decode the encoded polilyne returned by google with the Google maps SDK and translate it to a MKPolyline to show it in a MKMapView.
Here's a snippet in Obj-c for the second option:
GMSPath *path = [GMSPath pathFromEncodedPath:encodedPolyline];
if (path.count != 0){
CLLocationCoordinate2D points[path.count];
for (NSInteger i = 0; i < path.count; i++){
CLLocationCoordinate2D coordinate = [path coordinateAtIndex:(NSUInteger) i];
points[i] = coordinate;
}
MKPolyline *p = [MKPolyline polylineWithCoordinates:points count:path.count];
[self.mapView addOverlay:p];
}
You have some limitation using the directions API, you can see it here: https://developers.google.com/maps/documentation/directions/usage-limits
You can use Directions API. You can plot multiple locations by setting a start point, waypoints(optional) and an endpoint. For example you are to plot 5 different locations, your current location(startpoint), then three waypoints or stopovers and the endpoint. Directions API automatically calculates distance based on the order in which you put the waypoints. Routes can recalculated if you use optimize:true, by doing so it will rearrange the waypoints which will give you the shortest possible distance from your start point to the endpoint. This is only effective for a maximum of 8 places because of the number of limits of waypoints. There is a workaround for this, the idea is to make the last waypoint(end route) of the first route to be the start point of the next route. Here is a link for your reference and the code which is also in the reference.
Related
I'm not a programmer but have been playing around with a side project for fun. I'm using Xcode 10, Swift 5. I've pieced together a handful of things through youtube and SO but I've searched and experimented with this for three days and I'm running into dead ends here.
I am trying to determine the distance between a user's current location and a preset point (in this case the airport). I am able to find the distance between two hard-coded locations. I am also able to find and print the user's current location in the console. But when I try to combine those I am struggling, most often getting the error
'Value of type 'CLLocationCoordinate2D' has no member 'distance''
The code that working is and gives me the distance in meters I would like is:
lazy var phx = CLLocation(latitude: 33.409016, longitude: -111.805576)
lazy var distanceFromPhoenixToVegas = las.distance(from: phx)
And this will print out the current location coordinates:
func printCoordinates(){
if let location = locationManager.location?.coordinate {
print (location)
}
}
If any one could offer guidance I would appreciate it.
Side note, there are a lot of similar questions on Stack Overflow. There are questions about getting a user's current location, and questions about getting distances between two points. I believe this is essentially what I am asking, but it was only answered by the original poster, and the answer seems not exactly correct to me. iOS & Swift: display distance from current location?
coordinate is of type CLLocationCoordinate2D You only need locationManager.location
if let location = locationManager.location {
print(phx.distance(from: location))
}
I have aGpsLocation model, this model has a latitude, a longitude and a radius property.
I want to find a GpsLocation based on its latitude and longitude and radius.
So lets say i am on location [52, 4], i want to find a GpsLocation instance that has these exact coordinates or is radius meters away.
I am using the geocoder gem but the near function does not do the job. Also tried monkey patching the near function so i can use a database column instead of a variable inside the query but still no luck, there are some mechanics that transform the lat/long to a range when providing a radius parameter.
Any help is appreciated
Calculating the distance between two points on a sphere can be done using the Haversine formula. There is a gem called haversine that can help. Or, if you're using PostgreSQL, there is also the earthdistance module.
However, you may find it easier to relax your definition of "near" somewhat, as dealing with circles can be annoying. Instead, consider using squares or rectangles.
Say you define "near" as within 10 miles. If you treat that as a plus or minus factor around the latitude and longitude of a given point, then you can do a simple query to find all nearby points at once, rather than a series of Haversine calculations. You query might look something like this:
# Only accurate in the U.S.
MILES_PER_LATITUDE = 69.0
MILES_PER_LONGITUDE = 55.0
min_latitude = thisLocation.latitude - (10.0 / MILES_PER_LATITUDE)
max_latitude = thisLocation.latitude + (10.0 / MILES_PER_LATITUDE)
min_longitude = thisLocation.longitude - (10.0 / MILES_PER_LONGITUDE)
max_longitude = thisLocation.longitude + (10.0 / MILES_PER_LONGITUDE)
nearby_points = GpsLocation.where(latitude: min_latitude..max_latitude).where(longitude: min_longitude..max_longitude)
I'm looking for some help on comparative OpenLayer functions for the following Google maps functions, can someone please let me know what these would be?
I'm currently using
For getting distance, using the distance matrix API:
http://maps.googleapis.com/maps/api/distancematrix
For getting latitude and longitude of the current address:
http://maps.googleapis.com/maps/api/geocode/json?address
Kindly check the attachment which are using for getting latitude, longitude and distance matrix.
Function names:
function getLatLng($add)
function getRoadDistance($from, $to)
--
things are a little bit more complicated in OL than they are with the google-api
Routing: if you have a small road network you can consider creating a Database in PostGIS and use pgrouting to get routing functions, you can find more on it here
Or if you want to use it on a bigger scale there some APIs that do it for you, for example yourNavigator, you'll have to make a get request with your coordinates like this
http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&flat=startLatitude&flon=startLongitude&tlat=destLatitude&tlon=destinationLongitude
there is also the OSRM for the same purpose.
to get the longitude and latitude based on an address you can use the geocoder for Openlayers 3
that's what I could think of right now, I hope it helps
I am working with the Google Maps API and have drawn GMSPolylines on the map. I know which "node" (lat and long position for a turning point) on the map is closest to the user's location, and I know the next upcoming nodes. Given that information, how could one obtain the distance from the user's current location to the nearest point on the closest path? In the diagram below, how could we get x when we know the three GPS coordinates?
try something like this (swift 4.0)
func isApproaching(_ to:CLLocation, from:CLLocation? = nil, byDistanceInMeters:Double) -> Bool {
guard let baseLocation = from ?? ThisIsMyDefaultUserLocationVariable.location else { return false }
let delta = to.distance(from: baseLocation) as Double
if delta < byDistanceInMeters { return true }
return false
}
and this is how to call it:
if isApproaching(lastLocation, byDistanceInMeters: 50.0) ) {
// this is where you are in 50m perimeter
} else {
// here you are outside
}
.
Looking at this as a geometry problem instead of a programming problem might make this a little easier. There probably exists a library or API that does this with little more than a couple lines of code, but this approach should still yield a result with little to no overhead.
Disclaimer: This approach only works with straight lines.
You have two points that are on your path and one point that is not on the path. Using some basic algebra you can find a line that is parallel to the path and runs through the user's location, and then invert that line to find the shortest line between the user's location and the path. Then it's simply finding the intersection of two lines.
One thing to note, the larger the distance between nodes then the less accurate the euclidean distance will be. This should be negligible for nodes closer than ~100 miles.
This is my first time posting a question here, but I have found a lot of help from other people's questions.
My question refers to the -distanceFromLocation: function in CLLocation. I had assumed that finding the distance between point A and point B would be the same as that between point B and point A, but I have found that it is not. I have tested this in a very simple case:
CLLocation *test1 = [[CLLocation alloc] initWithLatitude:37.529530 longitude:-122.259232];
CLLocation *test2 = [[CLLocation alloc] initWithLatitude:47.900002 longitude:-103.495102];
NSLog(#"%f vs %f",[test2 distanceFromLocation:test1],[test1 distanceFromLocation:test2]);
[test1 release];
[test2 release];
The output to this was 1907269.942754 vs 1908105.959114, a difference of almost 900 meters.
Though 900 meters may be a small percentage, I am trying to determine if the user is closer to something than an annotation and 900 meters can change that.
My question is, which one of these values, if any, is correct? Also if which location I test against is important, do I need to always test against the same location (e.g. user to destination and annotation to destination rather than destination to user and annotation to destination)?
Sorry that the sample is a bit out of context, as my project is rather large and this illustrates my problem. Any help would be appreciated as this has caused me quite a bit of confusion and frustration.
The error you're observing is 1 part in 2000. Presumably the algorithm used in this method is optimized for speed, so sorting a list of locations on distance is fast, rather than accurate to the millimeter.
If you need accurate results, don't use these methods but find another way. If you need reproducible results, order the arguments in a defined way, e.g. always start with the lower latitude.