In my app, on button click I am opening default iphone map application as follows:-
NSString *mystr=[[NSString alloc] initWithFormat:#"http://maps.google.com/maps?saddr=%#&daddr=%#",userlocation,desti_latlong];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
In that desti_latlong is destination latitude longitude string and userlocation is user's latitude longitude string found by corelocation(location manager).
But I want to add source location as "Current Location" string on this API .
If we use Iphone defualt map app directly we can give current location it will popup in blue string Can I make this from my app so that If user moves location updated on map.
thanx for any Help!
OHh yes i was writing directly Current Location but I need to write Current+Location.solved that
NSString *mystr=[[NSString alloc] initWithFormat:#"http://maps.google.com/maps?saddr=Current+Location&daddr=Destination"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
Related
I am using this code to display some location on map.
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(37.789658,-122.417479);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:#"My Place"];
// Set the directions mode to "Walking"
// Can use MKLaunchOptionsDirectionsModeDriving 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];
}
This code is taking me to the apple map app which is present in my iphone, this is ok but it is not taking me back to my app.
If any one know any solution to achieve this please suggest me.
!!Thanks in advance.!!
Unfortunately what you want cannot be done.
Once you send the user to another app, the only way for your app to become active again is for that app to specifically return the user to your app, or for the user to navigate back to your app.
The maps application as of now will not return the user to your app. There is nothing you can do.
Like Daniel said, there is nothing you can do if you choose to open Apple Maps.
However, if Google Maps App for iOS is an option,
Google Maps App can be opened with a callback URL specified.
Google Maps SDK
Check the section under Specify a callback URL
A banner will shows up on the top of Google Maps and once clicked on it will return the user back to your app (or whichever url you specified).
I already implemented google mapping functionality in my iphone application.It works fine but if the location to search is wrong string then it maps a location and it redirects to current location only after the search button clicks. How to implement that if the location is wrong then directly go to the current location? My code is like below
NSString *urlString = [[NSString stringWithFormat:#"http://maps.google.com/maps?z=14&q=%#",message] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
please give me a solution as soon as possible
Thanks in advance
Since you lose control the moment you leave your app, you should always perform the validity check before opening the URL by passing message to a CLGeocoder or a 3rd party Geocoder .
CLGeocoder Class Reference
Basically query for the location string inside your app, then format your url accordingly.
Hi I have an iPhone application using maps and locations.
I would like the user to be able to press a button that gives turn by turn directions to that location. I understand that i am not allowed to do this in app, but i was wondering if anyone could tell me if it is possible to make a link to the native google maps application that will enter directions to the location from the users current location.
If it is possible could you also tell me how?
any help would be appreciated.
Thanks
Sure, it's possible - you've just got to tell the system to open a google maps link, with parameters for the start and end address set.
You might want to try using the following google maps URL:
http://maps.google.com/maps?saddr=x,y&daddr=x,y
So you can see the two parameters are saddr (start address) and
daddr (destination address). You set these to a pair of coordinates, separated by a comma.
Here is a bit of code I wrote that will take the user from their current location to a specific location (hardcoded in my case).
This is the core location delegate method which is called once their location has been established.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy) {
[self.locationManager stopUpdatingLocation];
CLLocationCoordinate2D coords = newLocation.coordinate;
NSString *stringURL = [NSString stringWithFormat:#"http://maps.google.com/maps?saddr=%g,%g&daddr=50.967222,-2.153611", coords.latitude, coords.longitude];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
}
To set this all up, you could add a property of a location manager to your controller, and then when you want to set it up (say in viewDidLoad) intialise it like this:
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
Calling [[UIApplication sharedApplication] openUrl:url]; will send it to the system's URL handler, which will detect it's a google maps link, and open it up in maps.
Hope that helps
I currently have an application which will drop pins onto various points of interest on a map, all done programmatically on the iPhone.
What I am aiming to get done now is to allow a user to navigate to that location and get turn by turn directions by calling up the built in navigation tools on the iPhone.
Is there anyway that I can do this? I've scoured the developer center and searched both google and here and couldn't find anything about this.
You're not allowed to use google directions within your app. The only way you can do this is to send them to the google maps application as per:
- (void)getDirections {
CLLocationCoordinate2D start = { (startLatitude), (startLongitude) };
CLLocationCoordinate2D end = { (endLatitude), (endLongitude) };
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, end.latitude, end.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}
Been looking into this for a while today however I can't seem to get my head around how to get the users current location into a Google Maps URL to open the Google Maps app with directions populated.
I've taken this code from another tutorial and added it into my app. I need to get the "start" lat/lng from the below into my IBAction to open the Google Maps app.
- (void)locationUpdate:(CLLocation *)location {
// Lat/Lng of user (Needs to be sent to IBAction for btnDirections)
CLLocationCoordinate2D start = { location.coordinate.latitude, location.coordinate.longitude };
}
The below is my IBAction for my "Get Directions" button.
- (IBAction) onButtonClick:(id) sender {
if(sender == btnDirections) {
CLLocationCoordinate2D destination = { 52.48306771095311, -1.8935537338256836 };
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
newLocation.coordinate.latitude, newLocation.coordinate.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}
}
I have done this previously in another app, however that view included a map showing the user location which allowed me to get the lat/lng easily.
Thanks in advance.
Simply pass "Current+Location" as a parameter to the google maps application:
http://maps.google.com/maps?saddr=Current+Location&daddr=Heathrow+Airport+London
In my code, I have an URL of the format :
http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f
(So that's 'maps' added from yours).
Other than that, you can setup your location request to stop after a set precision, did you setup precise enough for your purpose?
Oh, last note, getting current location might not work in simulator.
You need to make the user's current location available to the onButtonClick: method. You are creating the start structure on each location update, but you are never making it available outside of that method.