2013-08-07 22 views
2

我是新來的Ios。我正在學習MapKit和CoreLocation。我的程序中有兩個View Controller,在第一個視圖控制器中有一個文本字段,用戶可以輸入地址。和一個按鈕。填充地址後,如果用戶點擊按鈕比它將重定向到第二個視圖控制器,這是地圖視圖控制器。將會有兩個註釋。一個用於用戶的當前位置,另一個用於用戶輸入的地址。 所以,你能告訴我如何獲得經緯度,用戶的輸入地址,並顯示該地圖視圖的註釋長。如何從用戶輸入的地址得到經緯度6

在此先感謝。

+0

嘿@ user1960149檢查我的答案。,,,,和接受 –

+0

#import #import

回答

7

這裏是代碼

#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"]; 
3

您可以使用Google地理編碼爲this。就像通過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; 
} 
+0

嗨在哪裏調用這個函數以及如何調用這個函數請重玩! –

-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 
+0

這將從當前位置獲取,而不是從用戶輸入的自定義地址獲取 –