Elton's Blog

如何在iPhone的应用中使用Google Map

by on 九.16, 2009, under iPhone

在iPhone中应用Google地图其实很简单, 下面的例子将告诉你如何通过经纬度或者一个地址在iPhone中打开一个Google地图。

下面这个例子展示如何用经纬度来打开Google地图:

1
2
3
4
5
6
7
8
9
//Using longitude and latitude to drop a pin on Google maps
	float longitude = 38.892219;
	float latitude = -77.034674;
 
	NSString *url = [NSString stringWithFormat: @"http://maps.google.com/?q=%f,%f",
	                     longitude,
	                     latitude];
 
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

注意这个例子中的openURL:方法。 iPhone中的程序经常使用这个方法来共享他们的功能。 任何一个iPhone程序都可以注册一个URL用来让其他应用程序通过这个URL打开此应用。 Google地图程序就注册了一个这样的“http://maps.google.com/?q”URL。 上面的例子执行后,就会关掉当前的应用程序,而打开Google地图。

下面这个例子展示如何用地址打开一个Google地图:

1
2
3
4
//URL to map address
NSString *url = @"http://maps.google.com/?q=Constitution+Avenue+Northwest+Washington+DC";
 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];




:, ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...