《OpenInGoogleMapsController》类旨在让iOS开发者轻松在谷歌地图中打开地图、显示街景位置或直接显示一组路线。类支持使用《x-callback-URL》标准,允许您在谷歌地图中直接添加“返回到我的应用”按钮,并支持多种回退策略,以便在用户未安装谷歌地图时自动在另一应用程序中打开地图。
OpenInGoogleMapsController
类使用了谷歌地图URL协议。如果您想了解类的内部工作原理,我们强烈建议您首先阅读URL方案文档。
您可以从OpenInGoogleMaps的GitHub页面下载《OpenInGoogleMapsController》类及一个演示如何使用它的示例应用程序。
要将该类添加到Xcode项目,只需将《OpenInGoogleMapsController》.m和.h文件拖放到Xcode中。确保在必要时调用#import OpenInGoogleMapsController.h
。
如果您想尝试样例应用,请在Xcode中打开OpenInGoogleMapsSample.xcodeproj
。您可能想在真实设备上运行它,因为模拟器内没有安装谷歌地图。
《OpenInGoogleMapsController》是一个单例类,您可以通过调用这个类的sharedInstance
类方法来引用它。
[[OpenInGoogleMapsController sharedInstance] <make calls here>]
谷歌地图和谷歌Chrome都支持x-callback-URL规范,这样可以轻松地在谷歌地图中添加“返回到我的应用”按钮。要添加一个回调URL:
comgooglemyapp
)。在您的OpenInGoogleMapsController
类中设置callbackURL
属性。
NSString myURLScheme = @"comexamplemyapp://";
NSURL myCallbackURL = [NSURL URLWithString:myURLScheme];
[OpenInGoogleMapsController sharedInstance].callbackURL = myCallbackURL;
现在,当您在Google Maps(或Google Chrome)中打开地图时,您应该看到一个按钮,当用户完成地图的浏览时,它会将用户重定向回您的应用程序。
您只需要在应用程序的一生中设置一次回调URL,它将在所有未来的OpenInGoogleMapsController
请求中使用。
如果用户没有安装Google Maps,您可以设置多个后备策略供OpenInGoogleMapsController
尝试,方法是设置fallbackStrategy
属性。
[OpenInGoogleMapsController sharedInstance].fallbackStrategy =
kGoogleMapsFallbackChromeThenAppleMaps;
您可以尝试的后备策略如下
kGoogleMapsFallbackNone
= 如果用户未安装Google Maps,则不做任何操作并返回NO
。这是默认值。kGoogleMapsFallbackAppleMaps
= 使用Apple的Map应用程序打开地图。kGoogleMapsFallbackChromeThenSafari
= 如果已安装,则使用Google Chrome打开地图,否则使用Safari中的Google Maps打开地图。kGoogleMapsFallbackChromeThenAppleMaps
= 如果已安装,则使用Google Chrome打开地图,否则使用Apple的Map应用程序。kGoogleMapsFallbackSafari
= 使用Safari中的Google Maps打开地图。如果您指定了回调URL,它也将传递到Google Chrome。
您只需要在应用程序的一生中设置一次后备策略,它将在所有未来的OpenInGoogleMapsController
请求中使用。
如果您想手动检测Google Maps是否安装,可以使用isGoogleMapsInstalled
属性。
BOOL isGoogleMapsInstalled = [OpenInGoogleMapsController sharedInstance].isGoogleMapsInstalled;
打开地图需要先创建一个GoogleMapDefinition
对象来定义您想要打开的地图。然后,您可以将定义对象传递给openMap
方法。如果它能够在某个应用程序中打开地图,则该方法将返回YES
;如果不能打开地图,它将返回NO
,这可能是因为您没有定义要搜索的内容,或者您的用户未安装Google Maps且您未指定后备策略。
GoogleMapDefinition *definition = [[GoogleMapDefinition alloc] init];
// Steps to define the definition.
[[OpenInGoogleMapsController sharedInstance] openMap:definition];
GoogleMapDefinition
类包括多个属性,其中一些可以设置为nil
(NSString *)queryString
:如果设置,将用于按名称搜索地点的查询字符串。(CLLocationCoordinate2D)center
:在纬/经度上定义地图的中心。如果同时指定了此内容和查询字符串,则此内容将用作搜索的中心。要清除此值,请将其设置为kCLLocationCoordinate2DInvalid
。(将其设置为nil将指定中心为0,0。)(GoogleMapsViewOptions)viewOptions
:可以设置在您的地图上一组按位或操作选项kGoogleMapsViewOptionSatellite
:显示卫星视图。kGoogleMapsViewOptionTraffic
:显示交通信息。kGoogleMapsViewOptionTransit
:显示交通信息。float zoomLevel
:定义地图的缩放级别。这可以是0到21.0之间的任何值。以下是一个示例,打开一个包含“交通事故”和“卫星”地图图层功能的地图:“123 Main Street, Anytown, CA”。
GoogleMapDefinition *definition = [[GoogleMapDefinition alloc] init];
definition.queryString = @"123 Main Street, Anytown, CA";
definition.viewOptions = kGoogleMapsViewOptionSatellite | kGoogleMapsViewOptionTraffic;
[[OpenInGoogleMapsController sharedInstance] openMap:definition];
打开街景位置需要创建一个GoogleStreetViewDefinition
类来定义您要打开的位置。然后,您可以向openStreetView
方法传递此定义。如果方法能够成功打开某些应用程序中的街景请求,它将返回YES
;如果不能,则返回NO
,可能是因为您没有定义一套坐标,或者您的用户未安装谷歌地图且未指定备用策略。
请注意,YES
值并不能保证您指定的坐标是有效的街景位置。
如果您的备用策略涉及不支持街景的应用程,则OpenInGoogleMapsController
类将在地图上打开一个缩放后的卫星视图。
以下示例展示了如何打开靠近泰姬陵的街景位置。
GoogleStreetViewDefinition *definition = [[GoogleStreetViewDefinition alloc] init];
definition.center = CLLocationCoordinate2DMake(27.1724439,78.0420174);
[[OpenInGoogleMapsController sharedInstance] openStreetView:definition];
GoogleStreetViewDefinition
类包含一个属性
(CLLocationCoordinate2D) center
:使用经纬度定义街景位置。要清除此值,将其设置为kCLLocationCoordinate2DInvalid
。(设置为nil将指定中心为0,0。)在谷歌地图中打开一系列方向需要创建一个GoogleDirectionsDefinition
类来定义您要在之间旅行的点集。然后,您可以向openStreetView
方法传递此定义。如果能够成功打开一系列方向,则此方法将返回YES
;如果不能,将返回NO
,可能是因为您的起点和终点都是空的,或者您的用户未安装谷歌地图且未指定备用策略。
请注意,YES
值并不能保证谷歌地图(或其他备用应用程序)能够在这两个点之间找到一系列方向
GoogleDirectionsDefinition
类使用GoogleDirectionsWaypoint
类来定义方向请求的起点和终点。此类包含以下属性
CLLocationCoordinate2D location
:使用坐标集定义位置。NSString *queryString
:使用查询字符串(如地址)定义位置。如果这两个值都设置了,则位置优先于查询字符串。
GoogleDirectionsWaypoint
类还有两个类辅助方法:`+ waypointWithQuery:(NSString *)queryString`和`+ waypointWithLocation:(CLLocationCoordinate2D)location`,可以轻松构建航点。
GoogleDirectionsDefinition
类包含以下属性
GoogleDirectionsWaypoint *startingPoint
:定义起点。如果设置为nil
,则方向将从用户的当前位置开始。GoogleDirectionsWaypoint *destinationPoint
:定义终点。如果设置为nil
,则方向将在用户的当前位置结束。GoogleMapsTravelMode travelMode
:定义用户如何从startingPoint
到达destinationPoint
。当前选项包括kGoogleMapsTravelModeDriving
:开车。kGoogleMapsTravelModeTransit
:乘坐公共交通。kGoogleMapsTravelModeBiking
:骑行。kGoogleMapsTravelModeWalking
:步行。以下示例将帮助您规划下一次以玉米飯为中心的公路旅行。
GoogleDirectionsDefinition *definition = [[GoogleDirectionsDefinition alloc] init];
definition.startingPoint = [GoogleDirectionsWaypoint
waypointWithQuery:@"La Taqueria, 2889 Mission St San Francisco, CA 94110"];
definition.destinationPoint = [GoogleDirectionsWaypoint
waypointWithQuery:@"Delicious Mexican Eatery, 3314 Fort Blvd, El Paso, TX 79930"];
definition.travelMode = kGoogleMapsTravelModeDriving;
[[OpenInGoogleMapsController sharedInstance] openDirections:definition];
以下示例将为您提供从MI6总部到福尔摩斯地址的骑行路线。
GoogleDirectionsDefinition *definition = [[GoogleDirectionsDefinition alloc] init];
definition.startingPoint = [GoogleDirectionsWaypoint
waypointWithLocation:CLLocationCoordinate2DMake(51.487242,-0.124402)];
definition.destinationPoint = [GoogleDirectionsWaypoint
waypointWithQuery:@"221B Baker Street, London"];
definition.travelMode = kGoogleMapsTravelModeBiking;
[[OpenInGoogleMapsController sharedInstance] openDirections:definition];
以下示例将为您提供从您当前位置到北美国际车展的步行路线。
GoogleDirectionsDefinition *definition = [[GoogleDirectionsDefinition alloc] init];
definition.startingPoint = nil;
GoogleDirectionsWaypoint *destination = [[GoogleDirectionsWaypoint alloc] init];
destination.queryString = @"1 Washington Blvd, Detroit, MI 48226";
definition.destinationPoint = destination;
definition.travelMode = kGoogleMapsTravelModeWalking;
[[OpenInGoogleMapsController sharedInstance] openDirections:definition];
您可以在Docs/html/
文件夹或网上找到参考文档。它适合在深夜阅读。
特别感谢Ian Barber、Leo Hourvitz和Sam Thorogood对本文档的全面审查。任何剩余的错误均由作者负责。
Safari是苹果公司的注册商标。
参考文档由appledoc生成。