Reputation: 23935
I am using UIMapView to display locations on the iPhone. I want to do a directions from current location to the location of interest, I don't think its possible using MapKit (but if it is please inform) So I will open either the Google Maps application or safari to display it.
Can i do this by specifying co-ordinates from (current location) to co-ordinates (the location of interest) I have these longitudes and latitudes. Or do i have to use street addresses?
If I do have to use street addresses, can i get them from the latitude and longitude.
Upvotes: 23
Views: 58557
Reputation: 15951
First check google map is installed in device or not
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?saddr=23.0321,72.5252&daddr=22.9783,72.6002&zoom=14&views=traffic"]];
} else {
NSLog(@"Can't use comgooglemaps://");
}
Add query schema in .plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
Upvotes: 3
Reputation: 1647
Use bellow code for both google and apple maps in Swift 3 -
if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)
{
let urlString = "http://maps.google.com/?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving"
// use bellow line for specific source location
//let urlString = "http://maps.google.com/?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving"
UIApplication.shared.openURL(URL(string: urlString)!)
}
else
{
//let urlString = "http://maps.apple.com/maps?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d"
let urlString = "http://maps.apple.com/maps?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d"
UIApplication.shared.openURL(URL(string: urlString)!)
}
Upvotes: 13
Reputation: 6639
Yeah, it's not possible using MapKit. You could try to form a Google maps url request that contains both your current location and destination that will open in the Google maps app with the directions.
Here's an example url:
http://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944
Here's how you could implement this in your code:
CLLocationCoordinate2D start = { 34.052222, -118.243611 };
CLLocationCoordinate2D destination = { 37.322778, -122.031944 };
NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
Upvotes: 76
Reputation: 19
It is possible to show route in MapKit: Just use MKPolyline
I get polyline string from googleMapsApi. I parse it on server with php, and return final polilyne string to my app.
NSMutableArray *points = [myApp decodePolyline:[route objectForKey:@"polyline"]];
if([points count] == 0)
{
return;
}
// while we create the route points, we will also be calculating the bounding box of our route
// so we can easily zoom in on it.
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;
// create a c array of points.
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [points count]);
for(int idx = 0; idx < points.count; idx++)
{
// break the string down even further to latitude and longitude fields.
NSString* currentPointString = [points objectAtIndex:idx];
NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
// create our coordinate and add it to the correct spot in the array
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
MKMapPoint point = MKMapPointForCoordinate(coordinate);
if (idx == 0) {
northEastPoint = point;
southWestPoint = point;
}
else
{
if (point.x > northEastPoint.x)
northEastPoint.x = point.x;
if(point.y > northEastPoint.y)
northEastPoint.y = point.y;
if (point.x < southWestPoint.x)
southWestPoint.x = point.x;
if (point.y < southWestPoint.y)
southWestPoint.y = point.y;
}
pointArr[idx] = point;
_currentLenght++;
}
// create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:points.count];
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y,
northEastPoint.x - southWestPoint.x,
northEastPoint.y - southWestPoint.y);
// clear the memory allocated earlier for the points
free(pointArr);
if (nil != self.routeLine) {
[self.mapView addOverlay:self.routeLine];
}
[self.mapView setVisibleMapRect:_routeRect];
And showing:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.fillColor = [UIColor blueColor];
self.routeLineView.strokeColor = TICNavigatorColor;
self.routeLineView.lineWidth = 7;
self.routeLineView.lineJoin = kCGLineJoinRound;
self.routeLineView.lineCap = kCGLineCapRound;
overlayView = self.routeLineView;
}
return overlayView;
}
Give it a try.
Upvotes: 1
Reputation: 2348
It is possible.Use MKMapView Get the location coordinate where you tapped on the phone and using the two coordinates request the KML file from the google web service ,parse the KML file (sample app KML viewer in developer site) and display the routes....
Thank you
Upvotes: 2
Reputation: 561
A solid solution is to create a view controller with a NIB that includes a UIWebView, and then pass the URL that exercises Google's map / direction services. This way, you keep the user in the application. This approach is not sufficient when pulling up a web page, because the Apple kit doesn't support zooming. But with OS4, at least the user can double click the home button and switch back to the app.
Upvotes: 1
Reputation: 1
You can email the dropped pin to yourself and when you open the link in email, it will show the coordinates.
Upvotes: -1