使用谷歌地图 API iOS 进行反向地理编码

2024-04-15

我正在使用以下代码进行反向地理编码

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {   
    curLoc=newLocation;
    if (curLoc != nil) {
        latitude=curLoc.coordinate.latitude;
        longitude=curLoc.coordinate.longitude;
        //[self loadMap:latitude second:longitude];
        [self MarkerPoint:latitude+0.04 second:longitude+0.1 third:latitude-0.04 forth:longitude-0.1];

        NSError *error;
        NSString *lookupString = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude,longitude]];
        NSLog(@"URL: %@",lookupString);
        lookupString = [lookupString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

        NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookupString]];

        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];

        self.locationArray = [[jsonDict valueForKey:@"results"] valueForKey:@"formatted_address"];
        int total = self.locationArray.count;
        NSLog(@"locationArray count: %d", self.locationArray.count);

        for (int i = 0; i < total; i++)
        {
            NSString *statusString = [jsonDict valueForKey:@"status"];
            NSLog(@"JSON Response Status:%@", statusString);
            NSLog(@"Address: %@", [self.locationArray objectAtIndex:i]);
        }
    }
}    

现在我收到此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0xbce98c0'
*** First throw call stack:
(
    0   CoreFoundation                      0x034551e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x031a98e5 objc_exception_throw + 44
    2   CoreFoundation                      0x034f2243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0344550b ___forwarding___ + 1019
    4   CoreFoundation                      0x034450ee _CF_forwarding_prep_0 + 14
    5   MatchPoint                          0x00017fad -[ICSViewController locationManager:didUpdateToLocation:fromLocation:] + 813
    6   CoreLocation                        0x0164655e CLLocationCoordinate2DGetDistanceFrom + 18294
    7   CoreLocation                        0x01645af4 CLLocationCoordinate2DGetDistanceFrom + 15628
    8   CoreLocation                        0x01642e40 CLLocationCoordinate2DGetDistanceFrom + 4184
    9   CoreLocation                        0x0163c680 CLClientInvalidate + 996
    10  CoreFoundation                      0x034140b0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    11  CoreFoundation                      0x033dd339 __CFRunLoopDoBlocks + 361
    12  CoreFoundation                      0x033fb753 __CFRunLoopRun + 2355
    13  CoreFoundation                      0x033fa9d3 CFRunLoopRunSpecific + 467
    14  CoreFoundation                      0x033fa7eb CFRunLoopRunInMode + 123
    15  GraphicsServices                    0x051f05ee GSEventRunModal + 192
    16  GraphicsServices                    0x051f042b GSEventRun + 104
    17  UIKit                               0x01e69f9b UIApplicationMain + 1225
    18  MatchPoint                          0x0001d9fd main + 141
    19  libdyld.dylib                       0x03d206d9 start + 1
    20  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

通过你的纬度和经度它会给你正确的地址......

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
            NSLog(@"reverse geocoding results:");
           for(GMSAddress* addressObj in [response results])
            {
                NSLog(@"%@",[response results]);

                NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude);
                NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude);
                NSLog(@"thoroughfare=%@", addressObj.thoroughfare);
                NSLog(@"locality=%@", addressObj.locality);
                NSLog(@"subLocality=%@", addressObj.subLocality);
                NSLog(@"administrativeArea=%@", addressObj.administrativeArea);
                NSLog(@"postalCode=%@", addressObj.postalCode);
                NSLog(@"country=%@", addressObj.country);
                NSLog(@"lines=%@", addressObj.lines);
            }
        }];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用谷歌地图 API iOS 进行反向地理编码 的相关文章

  • 如何找到键盘未覆盖的视图部分(UIModalPresenationStyleFormSheet)?

    我有一个视图控制器 显示带有 UITextView 的视图 并且我想在键盘出现时调整视图的大小 以便 UITextView 不会被键盘覆盖 我几乎在所有情况下都可以正常工作 据我所知 仅当视图控制器以 ModalPresentationSt
  • .showsPhysics 内存泄漏

    我最近花了 5 个小时尝试调试 Spritekit 应用程序中的内存泄漏 应用程序启动后 我注意到内存使用量略有上升 我花了 5 个小时中的 3 个小时挖掘参考资料 了解强与弱的关系ARC https developer apple com
  • 如何在iOS中处理1到3个手指的滑动手势

    我使用以下代码来处理代码中的 1 根手指滑动 UISwipeGestureRecognizer swipe UISwipeGestureRecognizer alloc initWithTarget self action selector
  • 如何从 ContentView 外部显示 SwiftUI 警报?

    我正在构建 Swift 应用程序 并试图找出如何显示警报 我有一个单独的 swift 文件正在执行一些计算 并且在某些条件下我希望它向用户显示警报 基本上告诉他们出了问题 然而 我见过的大多数例子都要求警报在ContentView或以其他方
  • 从 UIPickerView 的选定行设置 UIButton 的标题

    详细场景是这样的 我使用循环创建 10 个按钮并设置 0 9 的标签 点击每个按钮时 我将调用 UIPickerView 在其中加载来自不同数组的数据 到这里我就得到了预期的结果 但我希望 pickerView 中选定的行应设置为相应按钮的
  • React-native-vision-camera无法访问后面的普通摄像头

    我正在尝试在 iPhone 11 Pro 上使用 普通 相机 我使用反应本机视觉相机 当我运行这段代码时 const devices useCameraDevices const deviceBack devices back consol
  • locationOfTouch 和 numberOfTouches

    你好 我有这个识别器 设置为 2 次触摸 但它只返回一个 而不是两个 CGPoint void gestureLoad UIGestureRecognizer recognizer recognizer UITapGestureRecogn
  • 每 24 小时触发一次方法

    我正在尝试每天在给定时间触发一个方法 我尝试了一些方法 但我无法真正使其发挥作用 任何意见 将不胜感激 此外 如果无论应用程序是否打开它都会触发 那就更理想了 这可能吗 UI本地通知 http developer apple com lib
  • Objective-C NSString for 循环与characterAtIndex

    我试图逐个字符地循环遍历 NSString 但出现 EXC BAD ACCESS 错误 您知道如何正确执行此操作吗 我已经在谷歌上搜索了几个小时但无法弄清楚 这是我的代码 m self textLength self text length
  • 使用未解析的标识符“FlurryAdInterstitial”

    我正在尝试整合Flurry Interstitial Ads使用cocoapods in Swift and Xcode 7 1 1 我正在关注开发人员雅虎网站上的此文档 https developer yahoo com flurry d
  • 在 iPhone 3GS 与 iPhone 4 上为 Mobile Safari 嵌入 HTML5 视频

    我在服务器上的 mp4 文件中有 H 264 AAC 编码的视频 mime 类型的视频 mp4 添加到 Web 服务器 IIS 7 并且我有一个带有视频标签的页面
  • iOS 7 上 Safari 浏览器的用户代理

    我只想在带有 Safari 浏览器的 iPhone 和 iPod 中打开我的网站 对于 Chrome Dolphin 等任何其他浏览器 它不应该打开 但目前我从几乎所有设备获得相同的用户代理 对于Safari User Agent Stri
  • 调整 UIImage 的大小而不将其完全加载到内存中?

    我正在开发一个应用程序 用户可以在其中尝试加载非常非常大的图像 这些图像首先在表格视图中显示为缩略图 我的原始代码会在大图像上崩溃 因此我重写它以首先将图像直接下载到磁盘 是否有一种已知的方法可以调整磁盘上图像的大小 而无需通过以下方式将其
  • UIView animateWithDuration:delay: 工作很奇怪

    我在使用 iPhone 动画块时遇到了一个奇怪的问题 这段代码 UIView animateWithDuration 2 delay 0 options 0 animations void controller setBackgroundC
  • “预期的 ';'在 Swift 下的顶级声明符之后”

    我正在尝试将所有颜色设置在一个 Swift 文件中 该文件可以在我的整个应用程序中使用 下面的代码会导致 import Foundation import UIKit class DotColors let tsblueColor UICo
  • 访问目标 c 中的类方法。使用 self 还是类名?

    我正在学习 iOS 编程 并且对以下有关关键字 self 的使用的代码感到困惑 据我了解 self就像Java的this 它指的是当前实例 当我想调用类方法时 通常的方式应该是这样 PlayingCard validSuits 但是侵入实例
  • 在实例化对象之前是否可以检查故事板中是否存在标识符?

    在我的代码中我有这一行 但我想知道是否有办法检查是否 一些控制器 在我将它与 一起使用之前就存在实例化ViewControllerWithIdentifier 方法 如果标识符不存在 则应用程序崩溃 如果没有好的方法 这并不是一个大问题 我
  • UIView 圆角 - Swift 2.0?

    我会尝试将一些项目更新到 Swift 2 0 我有一个视图 左上角有一个圆角 在 Swift 没有警告 没有错误 只是没有圆角 这就是它在 Swift let maskPath UIBezierPath roundedRect conten
  • 叠加 SKScene 未显示

    我正在尝试将 SKScene 覆盖在 SCNScene 上 当我在模拟器和 iPhone6 上运行我的应用程序时 overlayScene SKScene 按预期显示 但是当我尝试在 iPhone5 上运行它 尝试了 2 个不同的设备 时
  • iOS:如何知道 reloadData() 已完成其任务?

    我想滚动到给定索引 self boldRowPath 但是当我调试时scrollToRow之前执行reloadData 如何知道reloadData已完成 func getAllTimeEvent self arrAllTimeEvent

随机推荐