3

我是 iOS 的新手。我正在学习 MapKit 和 CoreLocation。我的程序中有两个视图控制器,在第一个视图控制器中有一个文本字段,用户可以输入地址。和一个按钮。填写地址后,如果用户点击按钮,它将重定向到第二个视图控制器,即地图视图控制器。会有两个注解。一个用于用户当前位置,另一个用于用户键入的地址。所以你能告诉我如何获取用户输入的地址的纬度,长度,并在地图视图上显示该纬度的注释。

提前致谢。

4

3 回答 3

8

这是代码

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

+(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {

    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    return center;

}

然后调用这个方法

coordinates = [self getLocationFromAddressString:@"address"];
于 2013-08-07T09:40:13.693 回答
3

您可以为此使用 Google 地理编码。它就像通过 HTTP 获取数据并对其进行解析一样简单(它可以返回 JSON KML、XML、CSV)。

或者

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    return center;
}
于 2013-08-07T09:37:07.670 回答
-1
    #import core location Framework

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

    //Current Location

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLLocationAccuracyKilometer;
        [locationManager startUpdatingLocation];

        CLLocation *location = [locationManager location];
        coordinate = [location coordinate];
        float latitude = coordinate.latitude;
        float longitude = coordinate.longitude;
        NSLog(@"%f",latitude);
        NSLog(@"%f",longitude);


    and type location you call google webservice and pass type address and
    google api return corrdinate for type address

    http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
于 2013-08-07T10:11:29.567 回答