get all coordinates between location for iphone Map Kit
Can anyone tell me how to get all Lat,Long values between to locations...??? As part of implementation I would like to have all the Lat,Long values between the start end end poi开发者_如何学Gont to draw the route.
Thanks in advance...
Finding the points is pretty straightforward and doesn't require any clever trigonometry. Note that if the distance between points needs to be a specific amount -- say every 100 meters -- then trigonometry will be required. We'll do the simple version, though.
Basically you figure out the difference between the latitudes and the difference between the longitudes, decide how many points you want (as noted, "all" is an infinite number), divide the distance by the number of points, and iterate through them. (Note: I haven't tested this code, but you should be able to get the idea.)
// Making my own names for your start and stop coordinate, use your own
CLLocationCoordinate2D startPoint; // Your starting latitude and longitude
CLLocationCoordinate2D stopPoint; // Ending latitude and longitude
CLLocationCoordinate2D newPoint; // A newly-created point along the line
double latitudeModifier; // Distance to add/subtract to each latitude point
double longitudeModifier; // Distance to add/subtract to each longitude point
int numberOfPoints = 100; // The number of points you want between the two points
// Determine the distance between the lats and divide by numberOfPoints
latitudeModifier = (startPoint.latitude - endPoint.latitude) / numbernumberOfPoints;
// Same with lons
longitudeModifier = (startPoint.longitude - endPoint.longitude) / numbernumberOfPoints;
// Loop through the points
for (int i = 0; i < numberOfPoints; i++) {
newPoint.latitude = startPoint.latitude + (latitudeModifier * i);
newPoint.longitude = startPoint.longitude + (longitudeModifier * i);
// Do something with your newPoint here
}
What do you mean by "all"? Since lat/long pairs are doubles, you are talking about an infinite number of points.
If you want to draw a line between two points you can use a CAShapeLayer to draw a line on the map to connect them.
The word "route" however impies you want to know points that travel along a road. There again, what are you really asking for? Routes people could drive? Walk? Take a bus? MapKit itself does not help you with routing at all, you would have to look at other third party frameworks.
精彩评论