【问题标题】:geocodeaddressstring not working (objective c)geocodeaddressstring 不起作用(目标 c)
【发布时间】:2018-05-03 04:23:56
【问题描述】:

我正在使用此代码执行地理编码地址字符串,但每次它都给我相同的纬度经度和不同的地址。 我检查了地标计数,它是 1,所以它完全通过了它。 我正在使用这种格式(这不完整)来获取我的地址,并且我检查了地址是否以字符串形式出现。

NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@"

感谢您的帮助,谢谢。

[self.geocoderDes geocodeAddressString:address completionHandler:^(NSArray *placemarks2, NSError *error) {
    if ([placemarks2 count] > 0) {

        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordination = location.coordinate;
        NSLog(@" location latitude %f, location longitude %f",coordination.latitude, coordination.longitude);


    }else {
        NSLog(@"%@", error.debugDescription);
    }
}];

【问题讨论】:

    标签: objective-c geocode


    【解决方案1】:

    在你的 info.plist 中添加这个键

    <key>NSLocationAlwaysUsageDescription</key>
    <string>Your Description</string>
    

    1) 您可以尝试更改现有代码中的一行

    CLPlacemark *placemark = [placemarks2 lastObject];
    

    2) 使用地理编码器试试这个

           [self.geocoderDes geocodeAddressString:address completionHandler:^(NSArray *placemarks2, NSError *error)
     {
                if ([placemarks2 count] > 0) {
    
              CLPlacemark *placemark = [placemarks2 lastObject];
              NSArray *lines = placemark.addressDictionary[@"FormattedAddressLines"];
             CLLocation *location = placemark.location;
    
             NSString *str_lat = [NSString stringWithFormat: @"%f", location .coordinate.latitude];
             NSString *str_long = [NSString stringWithFormat: @"%f", location.coordinate.longitude];
             NSString *finalAddress = [NSString stringWithFormat:@" %@, %@, %@", lines, str_lat , str_long ];
    
             NSLog(@" Address %@", finalAddress );
    
    
                }
    
    
        if(error) {
                    NSLog(@"Error");
                    return;
                } 
            }];
    

    3)没有地理编码器

    此方法用于获取基于纬度和经度的用户位置,例如州名、城市名、国家/地区名称。

    -(CLLocationCoordinate2D) getLocationFromAddressStr: (NSString*) addressStr {
        double lat = 0, longi = 0;
        NSString *escapAddr =  [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *reqst = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", escapAddr ];
        NSString *finalResult = [NSString stringWithContentsOfURL:[NSURL URLWithString:reqst] encoding:NSUTF8StringEncoding error:NULL];
        if (finalResult) {
            NSScanner *scanner = [NSScanner scannerWithString:finalResult];
            if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
                [scanner scanDouble:&lat];
                if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                    [scanner scanDouble:&longi];
                }
            }
        }
        CLLocationCoordinate2D center;
        center.latitude=lat;
        center.longitude = longi;
        NSLog(@"Location Logitute : %f",center.latitude);
        NSLog(@"Location Latitute : %f",center.longitude);
        return center;
    
    }
    

    在你想要的地方调用这个方法

    CLLocationCoordinate2D center;
            center=[self getLocationFromAddressStr:@"mumbai"];
          double  latFrom=&center.latitude;
          double  longFrom=&center.longitude;
    
      NSLog(@"Logitute : %f",latFrom);
            NSLog(@"Latitute : %f",longFrom);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多