I want to figure out how Google Map's API to give us directions.
Create a sample tool where I can use my location as a starting point and choose another location and I can view directions (both driving and walking) in text and on map within my existing application.
I haven't used google map api so I am completely naive about this
can anyone help me with this?
try this,
NSString *urlstring=[NSString stringWithFormat:#"http://maps.google.com/?saddr=%f,%f&daddr=%f,%f",sourcelocation.latitude,sourcelocation.longitude,destinationlocation.latitude,destinationlocation.longitude];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlstring]];
What the code do is, it will show the route between your source and destination location on google map.
If you dont have the longitude then you can do it like:-
NSString *source=#"NorthCarolina";
NSString *destination=#"SouthCarolina";
NSString *urlstring=[NSString stringWithFormat:#"http://maps.google.com/?saddr=%#&daddr=%#",source,destination];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlstring]];
I hope this will help you.Happy coding:)
Related
I've looked around for answers to my question with no luck, so here it is:
How can I make my iPhone app launch Google Maps while loading directions from one place to another and show the map direction tracing instead of the default menu where you get a list of directions.
Here what I am currently doing:
_destination = [NSString stringWithString:#"http://maps.google.com/maps?saddr=&daddr=Place+I+want+to+go+to"];
NSURL *url = [NSURL URLWithString:_destination];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[_mapsView loadRequest:requestURL];
This code shows by default the list of directions to follow to get to some place. The user has to press the "Maps" button to see the map of the two places, which is very annoying for what I need to do in my current app.
Any ideas would be greatly appreciated,
Thanks
"http://maps.google.com/maps?saddr=&daddr=Place+I+want+to+go+to&output=embed&z=15"];
The output=embed will give you the map trace and the z= is for zoom level.
Enjoy
I am showing a map view using MapKit framework in my iPhone application. I am also showing the particular friends list Pin point in map view based on the location they are. I would like to enhance now to showing the road map direction from my location to a particular selected friend location. I know the lat and long for both source and destination, but i have draw route line in road direction, it should be road direction. Could someone help me on this?
Thank you!
MapKit does not expose a means of performing driving directions. So, it's not as simple as asking the map to display a course from location A to location B. You have two options:
1) Integrate with Google's API to get the driving directions, and overlay your own lines onto the MapKit map.
or
2) Simply direct your users out of app and delegate this functionality to the built in Map app.
I have no experience with the former, but the later is very easy. Simply:
CLLocationCoordinate2D location = [[map userLocation] location].coordinate;
double currentLat = location.latitude;
double currentLong = location.longitude;
NSString *googleUrl = [[NSString alloc] initWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", currentLat, currentLong, item.latitude, item.longitude];
NSLog(#"%#", googleUrl);
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:googleUrl]];
Actually there is no api supported by iPhone sdk to draw route on map. There a repo on github which is using google maps api to draw route on map by using map overlay. It has some limitation but you can take help from this repo - https://github.com/kishikawakatsumi/MapKit-Route-Directions
I want my app to redirect the user to the Maps apps on the iPhone. Search query is fine, it shows the user Current Location and nearby hospitals (hopital in french, it's not a typo), but it doesn't zoom in. I read that the zoom goes from 1 to 19, but even at 18, I still see the entire North America...
NSString *urlString = #"http://maps.google.com/maps?q=hopital&t=m&z=18";
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString:urlString]];
I have been reading quite a lot and can't find out why this parameter seems to be ignored.
Any idea? Thanks in advance...
After more searches (for other aspects of the app) and some try/error guesses, I have found an answer which, hopefully, will be helpful to someone else.
This string:
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps?sll=%f,%f&z=14&q=hopital", userCoordinate.latitude, userCoordinate.longitude];
displays the map with a nice zoom, the user's location is centered in the screen and shows relevant searches around. (userCoordinate is a CLLocationCoordinate2D location that contains the user's location.)
Note that with the parameter sll (instead of just ll) displays the same map area but doesn't display the search results. The word (in this case "hopital") is pre-populated in the search box.
I have an app that display locations on the MapView, and i want to get the direction of a specific location from my current location using the Directions APP on the iPhone. how can i do that?
This is a pretty old post from Jeff: http://iphonedevelopment.blogspot.com/2009/02/mapping-directions-from-your-app.html .
From your question, I assume you want to get directions using the Maps app.
To do this you'll need to code something like:
NSString *mapsUrl =
[NSString stringWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",
self.mapView.userLocation.location.coordinate.latitude,
self.mapView.userLocation.location.coordinate.longitude,
<destination address>];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: [mapsUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]];
where saddr is the start address (or lat, lon pair in this case) and daddr is the destination address.
I have done that before. It is basically more trouble than it's worth because you have to implement the directions yourself and every time the Map app gets new features added then the direction system in your app will become outdated. What you really want to do is create a link for the directions to open in the Map app. Owners always make the argument that they don't want the user to have to leave the app, but ultimately you want to provide the best utilities for the user and unfortunately linking to the map app is the way to go here.
You can link to the map app like this:
[[UIApplication sharedApplication] openURL:[NSURL
URLWithString:#"http://maps.google.com/whateveryoururlis"];
I have reviewed the Apple Documentation (http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html) on sending a user to the Google Maps App and I have a question, "How do I have it automatically bring up directions from the user's current location to a predetermined lat/long location?
The Apple Docs say to use "saddr=" and "daddr=" for the source and destination respectively, but it doesn't say how to acquire the user's current location. Is there some sort of keyword such as "saddr=Current" or "saddr=Here" that will get the user's location?
Obviously, this doesn't work:
[app openURL:[NSURL URLWithString:#"http://maps.google.com/maps?saddr=Here&daddr=Chicago"]];
..if I was trying to send the user to Chicago. Any thoughts?
SOURCE:
How to invoke iPhone Maps for Directions with Current Location as Start Address
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. Like so:
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
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]];
I don't think you can do this directly. But you can put coordinates into the parameters by using the format saddr=lat,long (e.g. addr=53.453209,-0.32930). So if you work out the user's current location in your app before you despatch to Google Maps, you get an approximation of the functionality.
You can just use Current+Location in the saddr part of the url. That works on iOS, but I can't find what works for Android.
Simply calling the Google Maps URL with just the daddr parameter set, makes Google Maps insert the user's current location automatically in the From field.
http://maps.google.de/maps?daddr=SOMEADDRESS
I believe that saddr=Current%20Location&daddr=... is what you want.
You can always use the reverse geocoder in iOS to get the current locations address (given that the user lets you acquire their location) and use it in the URL, here MKReverseGeocoder is a reference to the class used for reverse geocoding.
-Daniel
NSString* directionsURL = [NSString stringWithFormat:#"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, mapPoint.coordinate.latitude, mapPoint.coordinate.longitude];
if ([[UIApplication sharedApplication] respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL] options:#{} completionHandler:^(BOOL success) {}];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL]];
}
You can use CurrentLocation Coordinate in the origin part and destinationLocation Coordinate in the destination part in the url. That works on iOS...
https://maps.googleapis.com/maps/api/directions/json?origin=(originCoordinate)&destination=(destinationCoordinate)&mode=driving&key=YOUR-KEY