0

I have a view controller which loads a map, I have set the current location as default when the map loads, the question I have is, how do I get a url for this location, so that I can store it in a nsstring and use it at some other place, to add it to tweet sheet basically.

OccamsRazor
  • 79
  • 1
  • 1
  • 7

2 Answers2

1

You'll want to create an URL such as https://maps.google.com/maps??ll=55.123,12.234

Get the users coordinates from the MKMapView current users location

mapView.userLocation.coordinate

or if it's the current map center you are interested in

mapView.centerCoordinate

In practice it would look like this

CLLocationCoordinate2D myCoord = mapView.userLocation.coordinate;

NSString *url = [NSString stringWithFormat:@"http://maps.google.com/?ll=%f,%f",
                                                  myCoord.latitude,
                                                  myCoord.longitude];

NSLog(@"The URL is: %@", url);

The snippet gives me the following output:

The URL is: http://maps.google.com/?ll=55.936699,12.289743

The parameters for the Google Maps URL can be found through Google and has been dealt with on StackOverflow.

Community
  • 1
  • 1
Niels Castle
  • 8,039
  • 35
  • 56
  • Thanks for the answer,that would mean I would have to replace the values in the url every time?I'm new to xcode so if that sounds foolish bear with me.I tried using a clmapcordinate2d *mycord and assigning the latt and long values as you said to *mycord.But then how do I set the url to take mycord instead of the actual values.Is it possible to set q=mycord? – OccamsRazor Jun 15 '12 at 13:52
  • I updated the answer to show the creation of the string as well - I also switched to using the ll parameter to Google Maps. Let me know if you need a more detailed answer. – Niels Castle Jun 16 '12 at 08:50
  • Thats awesome man, this helped me a lot, but still there is one problem, the nslog output is not taking the coordinates of my current location, its taking 0,0 as the coordinate values? I have setshowuserlocation set to yes, so my coord should take the values of the current location coordinate right? – OccamsRazor Jun 16 '12 at 12:44
  • Depending on where you are calling the code it might be zero for the first couple of seconds until a location is established. If you are using the simulator you have to specify a "fake" location from the menus of the simulator. – Niels Castle Jun 16 '12 at 15:05
0

you must save the city and county in a string. then you can build a google link like this:

NSString *link = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%@,%@",city,country"];
Eray Geveci
  • 1,099
  • 4
  • 17
  • 39
  • Hey, thanks for the quick reply, but isn't there a way to fill in the city and country parameters automatically when the users current location is loaded,otherwise it will be kind of impractical for users in different areas. – OccamsRazor Jun 14 '12 at 21:00