DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Open Google Map And Point To A Location Specified By Its Coordinates

05.03.2009
| 14371 views |
  • submit to reddit
        Coordinates were obtained from CLLocationManager. Open Google Map and point to that location.

	CLLocationCoordinate2D loc = [newLocation coordinate];
	
	latitude.text = [NSString stringWithFormat: @"%f", loc.latitude];
	longitude.text	= [NSString stringWithFormat: @"%f", loc.longitude];
	altitude.text = [NSString stringWithFormat: @"%f", newLocation.altitude];
	
	NSString *mapUrl = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%f,%f", loc.latitude, loc.longitude]; 
	NSURL *url = [NSURL URLWithString:mapUrl]; 
	[[UIApplication sharedApplication] openURL:url]; 

More examples. If you want to show all Thai restaurants, you could do:
NSString *name = @"thai";

NSString *latlong = @”-33.874559,151.219575″;

NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%@&mrt=yp&ll=%@",
                             [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                             [latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

You can also set zoom level with "z" flag. (1-19, default 17)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

It's also possible to place custom pins on the map by using a KML (Keyhold Markup Language) file:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=http://objective-d.com/sample.kml"]];
</cdoe>