I wondered if it is possible to create navigation route within MKMapView on iOS 6 instead of opening the built-in Maps application?
I googled it but didn't find any answer.
This is the code I'm using now (that is opening the built-in Maps app):
-(IBAction) navigation
{
if (SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
NSString *destinationString = #"Afek, Israel";
NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",wedMapView.userLocation.coordinate.latitude,wedMapView.userLocation.coordinate.longitude, destinationString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
else
{
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
double latitude = 32.83431728836292;
double longitude = 35.128666162490845;
MKPlacemark *placemark = [[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil] autorelease];
//[mapItem setName:#"Name of your location"];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:#"שמורתה, קיבוץ אפק"];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = #{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:#[currentLocationMapItem, mapItem] launchOptions:launchOptions];
};
}
}
And this is the MKMapView I Have that I want to make turn by turn route when I press the blue map button (above the Waze button):
Thanks for the help.
iOS 6 does not provide the app with directions, it relies on the built-in Maps app to do that. So if you want to show directions with in your app you'll need to find a service for calculating the route. You can't use Google because they only allow you to use their data on their maps, not on Apple's. You could look into CloudMade and OpenStreetMap, but you'd have to give up using iOS 6's maps for that. They come with their open UI components.
Related
I'm facing a problem, I'm developing a hybrid application using phonegap and the customer wants the possibility to open the native map (Apple map for iOS and Google Map for Android) with a marker and then back to the application when finished. On android It's OK, the back button is already implemented, however my problem is for iOS. I already developed a little plugin to open the native map and place a marker with the following code :
CDVPluginResult* pluginResult = nil;
NSNumber * latN = [command.arguments objectAtIndex:0];
NSNumber * lonN = [command.arguments objectAtIndex:1];
double lat = [latN doubleValue];
double lon = [lonN doubleValue];
CLLocationCoordinate2D myCoordinate = CLLocationCoordinate2DMake(lat, lon);
MKPlacemark *myPlacemark = [[[MKPlacemark alloc] initWithCoordinate:myCoordinate addressDictionary:nil]autorelease];
MKMapItem *mapItem = [[[MKMapItem alloc] initWithPlacemark:myPlacemark]autorelease];
mapItem.name = #"My car";
[mapItem openInMapsWithLaunchOptions:nil];
if (latN != nil && lonN != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:#"ok"];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Is there a way to perform this on iOS ?
Thanks in advance,
Med.
I come back with a solution that I used in my case, after a lot of research I found that the only way to do it was to use the mapKit framework.
So I simply used cordova MapKit plugin (https://github.com/phonegap/phonegap-plugins/tree/master/iOS/MapKit) with some modifications because this version is not necessary up to date.
I setup a view with the map view and a toolbar with a back button on the top, so we can access the native map and then back to the application easily.
Med.
In my app there is a contact us view that will have a button to open the company's address. So My code works fine in iOS6 and I know it should be different for iOS 5 but couldn't find the proper way to do it.
Here is my code:
// Check for iOS 6
BOOL iOS6 = NO;
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
iOS6 = YES;
switch (buttonIndex) {
case 0:
if (iOS6)
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(26.375561, 50.170305);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:#"Industrial Projects Technologies"];
// Pass the map item to the Maps app
[mapItem openInMapsWithLaunchOptions:nil];
}
else
{
// Use the iOS 5 method
}
break;
Use this code for iOS5 for showing source to destination route
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://maps.apple.com/?daddr=%#,%#&saddr=%#,%#",latitudeOfDestinationLocation,longitudeOfDestinationLocation,latitudeOfSourceLocation,longitudeOfSourceLocation]]];
To show particular point in map
maps.google.com/?q=latitude,longitude
Example: maps.google.com/?q=26.375561,50.170305
You can also refer to this link.
i added MapView and and required condition for showing current location , and with button functionality added just this method from apple documentation.
- (IBAction)refreshMapMethod:(id)sender
{
double latitude = 39.281516;
double longitude =-76.580806;
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:#"Name of your location"];
[mapItem openInMapsWithLaunchOptions:nil];
/////
////////
CLLocation* fromLocation = mapItem.placemark.location;
// Create a region centered on the starting point with a 10km span
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(fromLocation.coordinate, 10000, 10000);
// Open the item in Maps, specifying the map region to display.
[MKMapItem openMapsWithItems:[NSArray arrayWithObject:mapItem]
launchOptions:[NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithMKCoordinate:region.center], MKLaunchOptionsMapCenterKey,
[NSValue valueWithMKCoordinateSpan:region.span], MKLaunchOptionsMapSpanKey, nil]];
}
after pressing button i am getting new view with all the functionality like add annotation on map view with long press, tracing path and many more .how this is happening with small function
any idea pleas help .
Both openInMapsWithLaunchOptions: and openMapsWithItems:launchOptions: will open the Apple Maps application (and put your application into the background). Probably this isn't what you intended.
I am trying to find how to provide directions to a certain location using the in app MapView. I want my app to pull the users current location(which I have accomplished), then I want the MapView to use that information to provide turn by turn directions to a certain destination. Is this even possible with iOS SDK 6.0 and xcode 4.5?
U can Use Google Directional Map api .simply Implement
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([_iLat doubleValue],[_iLong doubleValue]);
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:placeMark];
if([destination respondsToSelector:#selector(openInMapsWithLaunchOptions:)])
{
//using iOS6 native maps app
[destination openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];
}
I am using MapKit in my app and trying to pin a coordinate (in Simulator).
In the map view, I can see the pin but the map is not loading and I get the following error in the console:
/SourceCache/ProtocolBuffer_Sim/ProtocolBuffer-91/Runtime/PBRequester.m:687 server returned error:400
//First in your .h file make an object of mapview
MKMapView *mymapView;
// Then in viewDidLoad of your .m file add this
mymapView.mapType = MKMapTypeStandard;
mymapView.scrollEnabled = YES;
mymapView.zoomEnabled = YES;
[mymapView setDelegate:self];
mymapView.showsUserLocation = NO;
annotation *ann = [[annotation alloc]init]; //annotation is my custom class of markers
ann.title = #" ";
ann.subtitle = #" ";
ann.coordinate = yourCordinates;
[mymapView addAnnotation:ann];
//Make sure that you have successfuly added the MapKit framework
Is the map in the Map app loading? If you're talking about the google base map and it doesn't load in your app, but does in the official Map app, then there are lots of people who want to know how you did that because there is no known way to block those tiles.