0

所以这是我的最后一个查询。我已经拥有了我需要的所有组件,但是,我需要手机从地理编码器中创建一个字符串,以便将其输入谷歌地图。该字符串被传递到服务器,然后由不同的用户从该数据库抓取到另一部 iPhone。我已经完成了数据库获取/发布端,但我需要地理编码才能工作,以便它可以生成地址字符串。这是我的代码:我使用 Mark/LaMarche 的 mapkit 教程作为我的基础。我的问题是:我可以使用地理编码器而不必使用 MapKit 吗?那会比下面写的代码节省更多的代码吗?谢谢!

- (IBAction)findMe {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
[lm startUpdatingLocation];

progressBar.hidden = NO;
progressBar.progress = 0.0;
progressLabel.text = NSLocalizedString(@"Determining Current Location", @"Determining Current Location");

button.hidden = YES;
}
- (void)openCallout:(id<MKAnnotation>)annotation {
progressBar.progress = 1.0;
progressLabel.text = NSLocalizedString(@"Showing Annotation",@"Showing Annotation");
[mapView selectAnnotation:annotation animated:YES];
}
#pragma mark -
- (void)viewDidLoad {
mapView.mapType = MKMapTypeStandard;
//    mapView.mapType = MKMapTypeSatellite;
//    mapView.mapType = MKMapTypeHybrid;
}
- (void)viewDidUnload {
self.mapView = nil;
self.progressBar = nil;
self.progressLabel = nil;
self.button = nil;
}
- (void)dealloc {
[mapView release];
[progressBar release];
[progressLabel release];
[button release];
[address release];
[super dealloc];
    }
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
       fromLocation:(CLLocation *)oldLocation {

if ([newLocation.timestamp timeIntervalSince1970] < [NSDate timeIntervalSinceReferenceDate] - 60)
    return;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000); 
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[mapView setRegion:adjustedRegion animated:YES];

manager.delegate = nil;
[manager stopUpdatingLocation];
[manager autorelease];

progressBar.progress = .25;
progressLabel.text = NSLocalizedString(@"Reverse Geocoding Location", @"Reverse Geocoding Location");

MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
geocoder.delegate = self;
[geocoder start];
}
- (void)locationManager:(CLLocationManager *)manager 
   didFailWithError:(NSError *)error {

NSString *errorType = (error.code == kCLErrorDenied) ? 
NSLocalizedString(@"Access Denied", @"Access Denied") : 
NSLocalizedString(@"Unknown Error", @"Unknown Error");

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:NSLocalizedString(@"Error getting Location", @"Error getting Location")
                      message:errorType 
                      delegate:self 
                      cancelButtonTitle:NSLocalizedString(@"Okay", @"Okay") 
                      otherButtonTitles:nil];
[alert show];
[alert release];
[manager release];
}


 #pragma mark -
    #pragma mark Alert View Delegate Methods
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    progressBar.hidden = YES;
    progressLabel.text = @"";
    }
    #pragma mark -
#pragma mark Reverse Geocoder Delegate Methods
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error      
{
UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:NSLocalizedString(@"Error translating coordinates into location", @"Error translating coordinates into location")
                      message:NSLocalizedString(@"Geocoder did not recognize coordinates", @"Geocoder did not recognize coordinates") 
                      delegate:self 
                      cancelButtonTitle:NSLocalizedString(@"Okay", @"Okay") 
                      otherButtonTitles:nil];
[alert show];
[alert release];

geocoder.delegate = nil;
[geocoder autorelease];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
progressBar.progress = 0.5;
progressLabel.text = NSLocalizedString(@"Location Determined", @"Location Determined");

MapLocation *annotation = [[MapLocation alloc] init];
annotation.streetAddress = placemark.thoroughfare;
annotation.city = placemark.locality;
annotation.state = placemark.administrativeArea;
annotation.zip = placemark.postalCode;
annotation.coordinate = geocoder.coordinate;

NSString *firstTwo = [placemark.thoroughfare stringByAppendingFormat:@" %@",placemark.locality];

NSString *firstThree = [firstTwo stringByAppendingFormat:@", %@",placemark.administrativeArea];

NSString *makeAddress = [firstThree stringByAppendingFormat:@", %@",placemark.postalCode];



address = makeAddress;


NSLog(@"%@", address);

[mapView addAnnotation:annotation];

[annotation release];

geocoder.delegate = nil;
[geocoder autorelease];
}
#pragma mark -
#pragma mark Map View Delegate Methods
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
static NSString *placemarkIdentifier = @"Map Location Identifier";
if ([annotation isKindOfClass:[MapLocation class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
    if (annotationView == nil)  {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
    }            
    else 
        annotationView.annotation = annotation;

    annotationView.enabled = YES;
    annotationView.animatesDrop = YES;
    annotationView.pinColor = MKPinAnnotationColorPurple;
    annotationView.canShowCallout = YES;
    [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];


    progressBar.progress = 0.75;
    progressLabel.text = NSLocalizedString(@"Creating Annotation",@"Creating Annotation");

    return annotationView;
}
return nil;
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:NSLocalizedString(@"Error loading map", @"Error loading map")
                      message:[error localizedDescription] 
                      delegate:nil 
                      cancelButtonTitle:NSLocalizedString(@"Okay", @"Okay") 
                      otherButtonTitles:nil];
[alert show];
[alert release];
}
4

1 回答 1

0

我假设当您说“Geocoder”时,您实际上是指 MKReverseGeocoder 类。反向地理编码器是 Mapkit 框架的一部分,因此为了使用它,您必须将它包含到您正在使用它的文件中。但是,您不需要添加实际的 MapView 或任何东西,您只需获取反向地理编码器为您提供的信息,然后根据需要将其传递给您的服务器。我希望这回答了你的问题。

于 2011-03-12T15:53:54.860 回答