I am using google map sdk and now i want to open get direction in google map where there are multiple stops like i am at Location A and from that i want to go to locations B,C,D i am able to open google map with location A to B but not able to open it for A to B,C,D.
How can i do this i tried this
NSString *str1 =[NSString stringWithFormat:#"http://maps.google.com/?saddr=%#&daddr=%#&waypoints=%#&key=%#",originString,destinationString,strWayPoints,GOOGLE_API_KEY];
if([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"comgooglemaps://"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str1]];
}
//str1 =
http://maps.google.com/?saddr=18.518205,73.857431&daddr=18.518205,73.857431&waypoints=via:18.518205,73.857431|via:18.552248,73.901596|via:18.629764,73.934685&key=MYKEY
You should use Google Maps URLs that provides universal cross platform syntax to open maps in mobile apps or Google Maps website.
In directions mode, you can specify the origin, destination and multiple waypoints for your route. For more details have a look at the following page:
https://developers.google.com/maps/documentation/urls/guide#directions-action
Example of URL:
https://www.google.com/maps/dir/?api=1&origin=18.518205,73.857431&destination=18.518205,73.857431&waypoints=18.518205,73.857431%7C18.552248,73.901596%7C18.629764,73.934685
I hope this helps!
This is code which is now working fine for get direction. I am answering here so that other will get how to use it
- (IBAction)onClickNavigate:(id)sender {
NSString *strWayPoints = [NSString stringWithFormat:#"%f,%f", [[destLatArray objectAtIndex:0] doubleValue], [[destLongArray objectAtIndex:0] doubleValue]];
for(int j=0;j<destLatArray.count;j++){
if(j > 0)
strWayPoints = [NSString stringWithFormat:#"%#|%f,%f", strWayPoints, [[destLatArray objectAtIndex:j] doubleValue], [[destLongArray objectAtIndex:j] doubleValue]];
}
NSString *originString = [NSString stringWithFormat:#"%f,%f",[sourceLat doubleValue], [sourceLong doubleValue]];
NSString *destinationString = [NSString stringWithFormat:#"%f,%f", [[destLatArray objectAtIndex:0] doubleValue], [[destLongArray objectAtIndex:0] doubleValue]];
NSString *str = [NSString stringWithFormat:#"https://www.google.com/maps/dir/?api=1&origin=%#&destination=%#&waypoints=%#",originString,destinationString,strWayPoints];
if([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"comgooglemaps://"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
else
{
NSLog(#"You haven't installed the google map");
}
}
I've been trying to direct users to the app store (iTunes) to rate my app.
I can't get the right link. Users say that when they click the button, nothing happens.
This is the current link I'm using:
tms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=XXXXX
And in my code, which is for Adobe AIR, this is the line:
navigateToURL(new URLRequest("tms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=XXXXX"));
(Of course XXXXX is replaced with my App ID).
Appreciate any help.
Thanks.
This is core objective-c code. Its very self-explanatory. All you need to do is to check whether the device is running iOS 7 or prior. In iOS 7, the URL for rating page is different.
URL Format for iOS 7 or prior: itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=XXXXXXX
URL Format for above iOS 7: itms-apps://itunes.apple.com/app/idXXXXXX
In Objective-C, it looks like this:
NSString *cAppleID = #"YOUR_APP_ID";
NSString* iOS7AppStoreURLFormat = #"itms-apps://itunes.apple.com/app/id%#";
NSString* iOSAppStoreURLFormat = #"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%#";
NSString* url = [[NSString alloc] initWithFormat: ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0f) ? iOS7AppStoreURLFormat : iOSAppStoreURLFormat, cAppleID];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
In AS3, it would look more like this (iOSVersion getter from How to determine the IOS version from within an Flex mobile app (AIR)):
//Put in a Utils class
public static function get iOSVersion():uint {
var iosVersion:String = Capabilities.os.match( /([0-9]\.?){2,3}/ )[0];
return Number( iosVersion.substr( 0, iosVersion.indexOf( "." ) ) );
}
var osVersion:Number = Utils.iOSVersion;
var url:String;
if (osVersion >= 7.0) {
url = "itms-apps://itunes.apple.com/app/idXXXXXX";
} else {
url = "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=XXXXXXX";
}
navigateToURL(new URLRequest(url));
I create a UITextView and set text=#"中国,浙江省杭州市滨江区",
set dataDetectorTypes=UIDataDetectorTypeAddress,
then,long pressed, choose open map, it can found the address in GoogleMap.
But, the same address, i used openUrl can't find the address.
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
anybody who can tell me why? or iOS not use this url(http://maps.google.com/maps?q=%#)
Would you try with a different enconding? eg., NSUnicodeStringEncoding
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding]];
And what is the result of
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
?
Google Maps http:// calls don't use % seperators but rather +'s.
NSString *fixedAddress = [fullAddress stringByReplacingOccurencesOfString:#" " withString:#"+"];
NSString *googleCall = #"http://maps.google.com/maps?q=";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[googleCall stringByAppendingString:fixedAddress]]];
I, myself encountered this issue and fixed it with the preceeding code last night.
I have some code which attempts to make a call within an application, but it doesn't seem to be working:
UIApplication *myApp = [UIApplication sharedApplication];
NSString *theCall = [NSString stringWithFormat:#"tel://%#",phone];
NSLog(#"making call with %#",theCall);
[myApp openURL:[NSURL URLWithString:theCall]];
Sometimes, the variable phone is something such as #"(102) 222-2222". How can I make a call with a phone number like this? Do I need to manually extract the numbers out of it and get rid of all the extra punctuation?
Yup. You need to take those out yourself. Or you can use the snippet below...
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", cleanedString]];
Note: you may be tempted to use -stringByTrimmingCharactersInSet:, but that one only removes characters at the start and the end of the string, not if they appear in the middle.
To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
just an update on above answer.
Here's a simple method that can be used to make a call and return to the app after the call is finished.
Add the following to your .m file
- (void) dialNumber:(NSString*) number{
number = [#"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}
Then add the following code wherever you want to make the call from:
[self dialNumber:#"5031234567"];
I know it's possible to start the iPhone maps application by calling openURL on a google maps URL with parameters saddr and daddr with location strings or Lat/Long (see example below).
But I'm wondering if it's possible to make the start address be the "Current Location" maps bookmark so that I can use the Maps app's location handling code. My Google search has been pretty fruitless.
For example:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%#&daddr=%#", myLatLong, latlong]]];
Except with something to invoke the current location bookmark in place of myLatLong.
Pre iOS 6
You need to use Core Location to get the current location, but with that lat/long pair, you can get Maps to route you from there, to a street address or location. Like so:
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
// this uses an address for the destination. can use lat/long, too with %f,%f format
NSString* address = #"123 Main St., New York, NY, 10001";
NSString* url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",
currentLocation.latitude, currentLocation.longitude,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
Finally, if you do want to avoid using CoreLocation to explicitly find the current location, and want to use the #"http://maps.google.com/maps?saddr=Current+Location&daddr=%#" url instead, then see this link that I provided in comments below for how to localize the Current+Location string. However, you are taking advantage of another undocumented feature, and as Jason McCreary points out below, it may not work reliably in future releases.
Update for iOS 6
Originally, Maps used Google maps, but now, Apple and Google have separate maps apps.
1) If you wish to route using the Google Maps app, use the comgooglemaps URL scheme:
NSString* url = [NSString stringWithFormat: #"comgooglemaps://?daddr=%#&directionsmode=driving",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
2) To use Apple Maps, you can use the new MKMapItem class for iOS 6. See the Apple API docs here
Basically, you will use something like this, if routing to destination coordinates (latlong):
MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
destination.name = #"Name Here!";
NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey, nil];
[MKMapItem openMapsWithItems: items launchOptions: options];
In order to support both iOS 6+ and pre iOS 6 in the same code, I'd recommend using something like this code that Apple has on the MKMapItem API doc page:
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
} else {
// use pre iOS 6 technique
}
This would assume that your Xcode Base SDK is iOS 6 (or Latest iOS).
NSString* addr = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=Current Location&saddr=%#",startAddr];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
[url release];
This works but only when the iPhone/iPod language is set in English. If you want to support other languages you'll have to use a localized string to match the Maps bookmark name.
This works on iPhone:
http://maps.google.com/maps?saddr=Current Location&daddr=123 Main St,Ottawa,ON
You can use a preprocessor #define like:
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
to understand your iOS version. Then, I can use this code to support iOS 6, too:
NSString* addr = nil;
if (SYSTEM_VERSION_LESS_THAN(#"6.0")) {
addr = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
} else {
addr = [NSString stringWithFormat:#"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
}
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
Because of sandboxing, you don't have access to the Map application's bookmarks.
Instead, use Core Location to determine the current location yourself. Then use that location (the lat and long) in the URL you build to open Maps.
I recommend checking out CMMapLauncher, a mini-library that I built to launch Apple, Google, and other iOS mapping apps with a specific mapping request. With CMMapLauncher, the code to get the directions in your question would be:
[CMMapLauncher launchMapApp:CMMapAppAppleMaps
forDirectionsFrom:[CMMapPoint mapPointWithName:#"Origin"
coordinate:myLatLong]
to:[CMMapPoint mapPointWithName:#"Destination"
coordinate:latlong]];
As you can see, it also encapsulates the version checking required between iOS 6 & others.
Hey since iOS6 is out!
Apple did something remarkable in a bad way (from my point of view).
Apple's maps are launched and for devices running iOS 6 you should not use maps.google.com/?q= if you want the iDevice to open the native Plan app. Now it would be maps.apple.com/?q=.
So that developers don't have to much work, the friendly maps.apple.com server redirects all non-Apple devices to maps.google.com so the change is transparent.
This way we developpers just have to switch all google query strings to apple ones.
This is what I dislike a lot.
I had to implement that functionnality today so I did it. But I felt I should not just rewrite every url lying in mobile website to target Apple's maps server so I thought I'd just detect iDevices server-side and serve apple urls just for those. I thought I'd share.
I'm using PHP so I used the opensource Mobile Detect library : http://code.google.com/p/php-mobile-detect/
Just use the isiPad pseudo method as a boolean getter and you're done, you wont convert google into apple ;-)
$server=$detect->isiPad()?"apple":"google";
$href="http://maps.{$server}.com/?q=..."
Cheers!
You can do this now in html just from a url on your mobile device only. Here's an example. The cool thing is if you turn this into a qr code you have a way of someone getting directions to you from wherever they are just by scanning it on their phone.
The real solution can be found here. Z5 Concepts iOS Development Code Snippet
It just requires a little bit of encoding.
- (IBAction)directions1_click:(id)sender
{
NSString* address = #"118 Your Address., City, State, ZIPCODE";
NSString* currentLocation = #"Current Location";
NSString* url = [NSStringstringWithFormat: #"http://maps.google.com/maps?saddr=%#&daddr=%#",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIApplication *app = [UIApplicationsharedApplication];
[app openURL: [NSURL URLWithString: url]];
}
For iOS6 the apple docs recommend using the equivalent maps.apple.com URL Scheme
so use
http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f
instead of
http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f
to be backwards compatible the code would be
NSString* versionNum = [[UIDevice currentDevice] systemVersion];
NSString *nativeMapScheme = #"maps.apple.com";
if ([versionNum compare:#"6.0" options:NSNumericSearch] == NSOrderedAscending)
nativeMapScheme = #"maps.google.com";
}
NSString* url = [NSString stringWithFormat: #"http://%#/maps?saddr=%f,%f&daddr=%f,%f", nativeMapScheme
startCoordinate.latitude, startCoordinate.longitude,
endCoordinate.latitude, endCoordinate.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
there is a whole load of other supported parameters for the Apple Maps URL scheme : Apple URL Scheme Reference
you can use these iOS version detection macros if you have conditional code in other parts of your code. iOS version macros
If you don't want to ask for location permissions and don't have the lat and lng, use the following.
NSString *destinationAddress = #"Amsterdam";
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)]) {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count] > 0) {
MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];
MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];
NSArray *mapItems = #[mapItem, mapItem2];
NSDictionary *options = #{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:
[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:#YES
};
[MKMapItem openMapsWithItems:mapItems launchOptions:options];
} else {
//error nothing found
}
}];
return;
} else {
NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];
NSString *urlToOpen = [NSString stringWithFormat:#"http://maps.google.com/maps?saddr=%#&daddr=%#",
[sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
}
For ios5, the Current Location needs to be in the correct language. I use the LocalizedCurrentLocation from this post http://www.martip.net/blog/localized-current-location-string-for-iphone-apps
For ios6, I use the CLGeocoder to get the placemark and then open the map with it and the current location.
Remember to add CoreLocation.framework and MapKit.framework
For iOS6 Maps App, you can just use the same URL posted above
http://maps.google.com/maps?saddr=%f,%f&daddr=%#
but instead of the Google maps URL, you use the url with maps:// resulting in the following URL: maps://saddr=%f,%f&daddr=%#.
Using 'Current Location' doesn't seem to work, so I stayed with the coordinates.
Antother good thing: It's backwards compatible: On iOS5, it launches the Google Maps app.
With the current version of Google Maps, simply omit the sadr parameter:
saddr: … If the value is left blank, then the user’s current location will be used.
https://developers.google.com/maps/documentation/ios/urlscheme
My suggestion would be using OpenInGoogleMaps-iOS as this is an up to date choice (by November 2015), it supports cocoa pods installation and you are ready to go in a few clicks.
Install using: pod "OpenInGoogleMaps"
Require in header file using: #import "OpenInGoogleMapsController.h"
Sample code below:
/*In case the user does NOT have google maps, then apple maps shall open*/
[OpenInGoogleMapsController sharedInstance].fallbackStrategy = kGoogleMapsFallbackAppleMaps;
/*Set the coordinates*/
GoogleMapDefinition *definition = [[GoogleMapDefinition alloc] init];
CLLocationCoordinate2D metsovoMuseumCoords; //coordinates struct
metsovoMuseumCoords.latitude = 39.770598;
metsovoMuseumCoords.longitude = 21.183215;
definition.zoomLevel = 20;
definition.center = metsovoMuseumCoords; //this will be the center of the map
/*and here we open the map*/
[[OpenInGoogleMapsController sharedInstance] openMap:definition];
I answered this on a different thread. (Current Location doesn't work with Apple Maps IOS 6). You need to get the coordinates of the current location first, then use it to create the map url.
If you don't provide source location, it will take current location as source. Try below code-
let urlString = "http://maps.apple.com/maps?daddr=(destinationLocation.latitude),(destinationLocation.longitude)&dirflg=d"
}
UIApplication.shared.openURL(URL(string: urlString)!)