iPhone使用地理编码器创建一个字符串,以便将其输入谷歌地图

时间:2011-02-25 18:09:40

标签: iphone geocoding reverse-geocoding

所以这是我的上一次查询。我有我需要的所有组件,但是,我需要手机从地理编码器创建一个字符串,以便将其输入谷歌地图。此字符串传递给服务器,然后由不同的用户从该数据库中抓取到另一个iPhone中。我已经完成了数据库的get / post端,但是我需要使用地理编码才能生成地址字符串。这是我的代码:我使用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];
}

1 个答案:

答案 0 :(得分:0)

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