对多个位置进行地理编码并检查最接近用户的位置

时间:2014-11-18 01:22:06

标签: xcode mapkit geocoding

我有一组地址字符串,我需要进行地理编码,看看该位置是否接近用户位置。我只得到地理编码的第一个地址,其余的没有。我有一个方法,可以对位置与用户位置进行地理编码和检查,并将字符串添加到NSMutable数组中。和想法??

[self findCloseShacks:[self.utahAddress objectAtIndex:0]];
[self findCloseShacks:[self.utahAddress objectAtIndex:1]];
[self findCloseShacks:[self.utahAddress objectAtIndex:2]];

此方法进行地理编码并查找是否在10 km范围内

      -(void)findCloseShacks: (NSString *)address
    {
     [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) {
        NSLog(@"Geocode failed with error: %@", error);
        return;
    }


        CLPlacemark *placemark = placemarks[0];
        CLLocation *location = placemark.location;
        tempLat = location.coordinate.latitude;
        tempLong = location.coordinate.longitude;

        CLLocationDistance meters = [myUserLocation distanceFromLocation: location];

        if(meters > 10000)
        {
            [closest addObject:address];
            NSLog(@"added %@", address);

            MKPointAnnotation *pin = [[MKPointAnnotation alloc] init];
            CLLocationCoordinate2D locCoords = CLLocationCoordinate2DMake(tempLat, tempLong);
            [pin setCoordinate:locCoords];
            NSArray *titles = [self.utahShackAndAddress allKeysForObject:address];
            NSString *theTitle = [titles objectAtIndex:0];
            [pin setTitle:theTitle];
            [self.mapview addAnnotation:pin];
    }

}];

}

0 个答案:

没有答案