Using Current Location from Location Manager in a Google Maps openURL? - iphone

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.

Related

How to add infoWindow to users current location in Google Maps v1.3 for iOS?

I would like to add an infoWindow when the user taps their current location (blue dot) in the Google Maps API V1.3 for iOS.
I can get infoWindows on other markers but not to display above the users location. Any help would be appreciated. Thanks!
As an alternative option, could you add a marker with a transparent icon at the user's current location?
You could use KVO to move the marker whenever the map view's myLocation value changes.
You can see some sample code for how to add an observer here (but instead of moving the camera, you could move your invisible marker):
about positioning myself,some problems
when you go with the the 'default' user location, you can't. Because it isn't a real marker, the callbacks aren't fired.
What I did in our case is that I set showsMyLocation=NO but I rolled my own location. I added another marker and used a CLLocationManager to position it according to THAT position..
So in pseudocode
- viewDidLoad
map.showsUserLocation=NO;
locationManager = [CLLocationManager new];
locationManager.delegate = self;
}
- locationManager:didUpdateUserLocation:newLoc {
if(!annotation) {
annotation = [GMSMarker new];
annotation.map = map;
}
annotation.position = newLoc;
}
=> own annotation that you can treat like any other marker!

How to get back to my app after going to map app from my application in ios 6

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).

Opening native google maps in Xcode

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

How to get a map view in an iPhone app

How can I get this kind of map view in my application?
Any examples?
You'll need to use an MKMapView. For the popup, you'll need an annotation. Looking at the MKMapView documentation should get you pretty far..
You should be using MKMapView - as donkim mentioned do some reading on MKMapView, taht would get you pretty far. You can use MKAnnotations for the maps annotations. You can google for "custom annotations" if you want to want to use custom images for annotations.
You can refer to this post that i posted a while ago for a memory leak, but it pretty much has the code to show annotations on the map. I have borrowed the code from internet too...
I guess I need to call this... in
//NSString *latlong = [[NSString stringWithFormat:#"%f, %f", 52.366428f,4.896349f] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//........for destination location.
//NSString *dlatlong = [[NSString stringWithFormat:#"%f, %f", coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//for the current location. this. where coordinate is CLLocationManager2D.
//NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?ll=%#&saddr=%#&daddr=%#",
//latlong, latlong, dlatlong];
need not to use mkmapkit.
There is a easier way of show a location on the map, however this approach will not implement a map view inside your application but it will open the map application.
[[UIApplication sharedApplication] openUrl:#"http://maps.google.com/maps?q=Oslo"];
After the q- parameter key you can write the same string as you normally write in google maps specifying the address. Read the documentation for UIApplication class for more information on this. Note, this will also automatically make a annotation for the location.
Just mention this because others may find this a useful if they don't need the map inside the application.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html
Here is a list of other parameters:
http://mapki.com/wiki/Google_Map_Parameters

Xcode google map navigation

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]];
}