【问题标题】:Forward GeoCoding is Working on Simulator but not iPhoneForward GeoCoding 正在模拟器上工作,但不是 iPhone
【发布时间】:2014-01-04 08:22:43
【问题描述】:

所以我有一个简单的应用程序,它将地址作为字符串传递给视图,然后使用

    - (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    return center;
}

在模拟器上效果很好。一旦我将它加载到我的手机上,我放置的 pin 的坐标不再是地址的纬度/经度,而是 0,0。

发生了什么事?提前致谢。

【问题讨论】:

  • 你在什么操作系统上运行这个设备和模拟器?

标签: ios mkmapview ios-simulator geocoding clgeocoder


【解决方案1】:

像这样改变你的方法:

 - (CLLocationCoordinate2D)geoCodeUsingAddress:(NSString *)address
    {
        double latitude = 0, longitude = 0;
        NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *req = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false", esc_addr];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];
        NSError *err = nil;
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];

        if (!err)
        {
            NSLog(@"status = %@",[jsonDict objectForKey:@"status"]);
            if ([[jsonDict objectForKey:@"status"] isEqualToString:@"OK"])
            {
                latitude = [[[[[[jsonDict objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lat"] doubleValue];
                longitude = [[[[[[jsonDict objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lng"] doubleValue];
            }
        }

        CLLocationCoordinate2D center;
        center.latitude = latitude;
        center.longitude = longitude;
        return center;
    }

【讨论】:

  • 感谢工作!我也用当前代码重新启动了我的手机,它工作了一段时间。也许这也是硬件问题。
猜你喜欢
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多