0

これが私の最後のクエリです。必要なコンポーネントはすべて揃っていますが、電話でジオコーダーから文字列を作成して、Google マップにフィードできるようにする必要があります。この文字列はサーバーに渡され、別のユーザーによってそのデータベースから別の 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];
}
4

1 に答える 1

0

「ジオコーダー」と言うとき、実際には MKReverseGeocoder クラスを意味していると思います。リバース ジオコーダーは Mapkit フレームワークの一部であるため、これを使用するには、それを使用しているファイルに含める必要があります。ただし、実際の MapView などを追加する必要はありません。リバース ジオコーダーが提供する情報を取得して、必要に応じてサーバーに渡すことができます。これがあなたの質問に答えることを願っています。

于 2011-03-12T15:53:54.860 に答える