geocodeaddressstring不起作用(目标c)

时间:2018-05-03 04:23:56

标签: objective-c geocode

我使用此代码来执行geocodeaddressstring,但每次它给我相同的纬度经度与不同的地址。 我检查了地标计数,它是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);
    }
}];

1 个答案:

答案 0 :(得分:1)

在info.plist中添加此密钥

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

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

CLPlacemark *placemark = [placemarks2 lastObject];

2)使用Geocoder试试这个

       [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);