首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >IOS/iPhone示例代码,用于通过地名和/或通过使用Google Maps API的地图选择位置?

IOS/iPhone示例代码,用于通过地名和/或通过使用Google Maps API的地图选择位置?
EN

Stack Overflow用户
提问于 2011-12-09 08:24:52
回答 1查看 2.6K关注 0票数 0

有没有IOS/iPhone示例代码,可以通过placename和/或通过使用Google Maps API的地图来选择位置?

因此,让用户通过以下两者的用户选择位置的示例:-键入位置&让Google Maps API尝试在地图上查找位置和/或-移动地图上的位置标记-例如,您可以输入一个地点并在地图上靠近,然后使用地图最终结果调整精确位置-最终结果为Lat,Long

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-09 11:33:51

您所指的是正向地理编码,不幸的是,苹果还没有为它提供api。然而,还有一个最好的东西,第三方api。看看这个项目,我在两个项目中成功地使用了它,效果很好:https://github.com/bjornsallarp/BSForwardGeocoder

确保包含必要的文件,以便可以包含BSForwardGeocoder而不会出错。然后,只需在BSForwardGeocoderDelegate中创建要使用它的类,并在类中实现以下两个协议方法:

代码语言:javascript
复制
-(void)queryAddress
{
  // Initialize member BSForwardGeocoder object
  if (forwardGeocoder == nil) {
    forwardGeocoder = [[BSForwardGeocoder alloc] initWithDelegate:self];
  }

  // send the request

  [forwardGeocoder findLocation:@"Saint Paul, MN"];
}

和:

代码语言:javascript
复制
-(void)forwardGeocoderFoundLocation:(BSForwardGeocoder *)geocoder
{   
  /*
   * BSForwardGeocoder delegate method. Receives the callback upon geocoding data gather   completion.
   */
  if (forwardGeocoder.status == G_GEO_SUCCESS) {

    int searchResults = [forwardGeocoder.results count];

    for (int i = 0; i < searchResults; i++) {

      // See the sample project code fore other useful location properties...
      // Here I retrieve the returned lat and lon.

      Result *location = [forwardGeocoder.results objectAtIndex:i];
      mLatitude = location.latitude;
      mLongitude = location.longitude;
    } 
  } else { 
    NSString *message = @"";

    switch (forwardGeocoder.status) {
        case G_GEO_BAD_KEY:
            message =@"Bad Api Key";
            break;

        case G_GEO_UNKNOWN_ADDRESS:
            message = @"Address Not Found!";
            break;

        case G_GEO_TOO_MANY_QUERIES:
            message = @"Too Many Queries";   
            break;

        default:
            break;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information" 
                                                    message:message
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles: nil];  
    [ alert show ];
    [ alert release ];  
  }
}

这将使用sallarp提供的大型库返回明尼苏达州圣保罗的纬度和经度。

如果您想要在地图周围拖动注释并获取其坐标信息,也可以这样做,您需要将您的类设置为mkmapview委托并实现mapView:annotationView:didChangeDragState:fromOldState:方法。在您的实现中,首先检查newState是否等于mkAnnotationViewDragStateEnding,如果是,您可以通过annotationView.annotation.coordinate属性获取其坐标信息。当然,您需要将注解视图的draggable属性设置为YES。如果您愿意,您可以通过BSForwardGeocoder查询方法将这些NSString格式的坐标发送到谷歌,并获取其相关信息。

格雷格

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8439598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档