2015-04-16 99 views
2

我正在開發GPS應用程序,我需要根據經度和緯度來確定位置。我能夠確定經度和緯度以及位置,但我是以json格式獲取google map API調用的結果,其中有許多數據以及城市名稱。提取需要的信息從谷歌地圖放置IOS中的地圖API

我已經這樣做了。(我用的模擬器,所以我已經通過固定的經度和緯度)

NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017]; 
    NSError* error; 
     NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error]; 
     locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 

    NSLog(@"%@",locationString); 

輸出字符串我得到是

015-04-16 10:38 :45.056 demolocationapp [690:12005] {

results : [ 
     { 
     address_components : [ 
      { 
       long_name : Rauni Kalan Link Road, 
       short_name : Rauni Kalan Link Rd, 
       types : [ route ] 
      }, 
      { 
      **long_name : Rauni Kalan, 
       short_name : Rauni Kalan, 
       types : [ locality, political]** 

      }, 
      { 
       long_name : Rupnagar, 
       short_name : Rupnagar, 
       types : [ administrative_area_level_2, political ] 
      }, 
      { 
       long_name : Punjab, 
       short_name : PB, 
       types : [ administrative_area_level_1, political ] 
      }, 
      { 
       long_name : India, 
       short_name : IN, 
       types : [ country, political ] 
      }, 
      { 
       long_name : 140102, 
       short_name : 140102, 
       types : [ postal_code ] 
      } 
     ], 
     formatted_address : Rauni Kalan Link Road, Rauni Kalan, Punjab 140102, India, 
     geometry : { 
      bounds : { 
       northeast : { 
        lat : 30.8209903, 
        lng : 76.4550081 
       }, 
       southwest : { 
        lat : 30.8178417, 
        lng : 76.44540219999999 
       } 
      }, 
      location : { 
       lat : 30.8186057, 
       lng : 76.4507395 
      }, 
      location_type : APPROXIMATE, 
      viewport : { 
       northeast : { 
        lat : 30.8209903, 
        lng : 76.4550081 
       }, 
       southwest : { 
        lat : 30.8178417, 
        lng : 76.44540219999999 
       } 
      } 
     }, 
     place_id : ChIJc8IoHwICEDkR38Ie5TOeGu0, 
     types : [ route ] 
     }, 
     { 
     address_components : [ 
      { 
       long_name : Rauni Kalan, 
       short_name : Rauni Kalan, 
       types : [ locality, political ] 
      }, 
      { 
       long_name : Rupnagar, 
       short_name : Rupnagar, 
       types : [ administrative_area_level_2, political ] 
      }, 
      { 
       long_name : Punjab, 
       short_name : PB, 
       types : [ administrative_area_level_1, political ] 
      }, 
      { 
       long_name : India, 
       short_name : IN, 
       types : [ country, political ] 
      }, 
      { 
       long_name : 140102, 
       short_name : 140102, 
       types : [ postal_code ] 
      } 
     ], 
     formatted_address : Rauni Kalan, Punjab 140102, India, 
     geometry : { 
      bounds : { 
       northeast : { 
        lat : 30.8219531, 
        lng : 76.4462614 
       }, 
       southwest : { 
        lat : 30.81705139999999, 
        lng : 76.4414979 
       } 
      }, 
      location : { 
       lat : 30.8192965, 
       lng : 76.44401660000001 
      }, 
      location_type : APPROXIMATE, 
      viewport : { 
       northeast : { 
        lat : 30.8219531, 
        lng : 76.4462614 
       }, 
       southwest : { 
        lat : 30.81705139999999, 
        lng : 76.4414979 
       } 
      } 
     }, 
     place_id : ChIJ_f1PWh0CEDkRKlzFwhc62zo, 
     types : [ locality, political ] 
     }, 
     { 
     address_components : [ 
      { 
       long_name : 140102, 
       short_name : 140102, 
       types : [ postal_code ] 
      }, 
      { 
       long_name : Punjab, 
       short_name : PB, 
       types : [ administrative_area_level_1, political ] 
      }, 
      { 
       long_name : India, 
       short_name : IN, 
       types : [ country, political ] 
      } 
     ], 
     formatted_address : Punjab 140102, India, 
     geometry : { 
      bounds : { 
       northeast : { 
        lat : 30.869706, 
        lng : 76.47508049999999 
       }, 
       southwest : { 
        lat : 30.7970006, 
        lng : 76.3335855 
       } 
      }, 
      location : { 
       lat : 30.8288892, 
       lng : 76.4215988 
      }, 
      location_type : APPROXIMATE, 
      viewport : { 
       northeast : { 
        lat : 30.869706, 
        lng : 76.47508049999999 
       }, 
       southwest : { 
        lat : 30.7970006, 
        lng : 76.3335855 
       } 
      } 
     }, 
     place_id : ChIJHX2_rA0BEDkR_QslHcc_M0g, 
     types : [ postal_code ] 
     }, 
     { 
     address_components : [ 
      { 
       long_name : Fatehgarh Sahib, 
       short_name : Fatehgarh Sahib, 
       types : [ administrative_area_level_2, political ] 
      }, 
      { 
       long_name : Punjab, 
       short_name : PB, 
       types : [ administrative_area_level_1, political ] 
      }, 
      { 
       long_name : India, 
       short_name : IN, 
       types : [ country, political ] 
      } 
     ], 
     formatted_address : Fatehgarh Sahib, Punjab, India, 
     geometry : { 
      bounds : { 
       northeast : { 
        lat : 30.8779699, 
        lng : 76.63427999999999 
       }, 
       southwest : { 
        lat : 30.4275501, 
        lng : 76.0761799 
       } 
      }, 
      location : { 
       lat : 30.6810924, 
       lng : 76.4100267 
      }, 
      location_type : APPROXIMATE, 
      viewport : { 
       northeast : { 
        lat : 30.8779699, 
        lng : 76.63427999999999 
       }, 
       southwest : { 
        lat : 30.4275501, 
        lng : 76.0761799 
       } 
      } 
     }, 
     place_id : ChIJqf6CHWocEDkR4EljDGd5be8, 
     types : [ administrative_area_level_2, political ] 
     }, 
     { 
     address_components : [ 
      { 
       long_name : Punjab, 
       short_name : PB, 
       types : [ administrative_area_level_1, political ] 
      }, 
      { 
       long_name : India, 
       short_name : IN, 
       types : [ country, political ] 
      } 
     ], 
     formatted_address : Punjab, India, 
     geometry : { 
      bounds : { 
       northeast : { 
        lat : 32.4981352, 
        lng : 76.92175809999999 
       }, 
       southwest : { 
        lat : 29.537147, 
        lng : 73.8708879 
       } 
      }, 
      location : { 
       lat : 31.1471305, 
       lng : 75.34121789999999 
      }, 
      location_type : APPROXIMATE, 
      viewport : { 
       northeast : { 
        lat : 32.4981352, 
        lng : 76.92175809999999 
       }, 
       southwest : { 
        lat : 29.537147, 
        lng : 73.8708879 
       } 
      } 
     }, 
     place_id : ChIJVXOeVqpkGTkRYYijAzEmvY8, 
     types : [ administrative_area_level_1, political ] 
     }, 
     { 
     address_components : [ 
      { 
       long_name : India, 
       short_name : IN, 
       types : [ country, political ] 
      } 
     ], 
     formatted_address : India, 
     geometry : { 
      bounds : { 
       northeast : { 
        lat : 35.5087008, 
        lng : 97.395561 
       }, 
       southwest : { 
        lat : 6.7535159, 
        lng : 68.162386 
       } 
      }, 
      location : { 
       lat : 20.593684, 
       lng : 78.96288 
      }, 
      location_type : APPROXIMATE, 
      viewport : { 
       northeast : { 
        lat : 35.5087008, 
        lng : 97.395561 
       }, 
       southwest : { 
        lat : 6.7535159, 
        lng : 68.16288519999999 
       } 
      } 
     }, 
     place_id : ChIJkbeSa_BfYzARphNChaFPjNc, 
     types : [ country, political ] 
     } 
    ], 
    status : OK 
} 

我已經把我需要的數據,其中類型是局部性,在**必需文本**

0政治

我怎樣才能像

回答

4

使用這種方法

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if (!error) 
    { 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSArray *arr=[[json valueForKeyPath:@"results.address_components"] objectAtIndex:0]; 

     for (NSDictionary *dictAddress in arr) 
     { 
      if ([[dictAddress objectForKey:@"types"] count] > 0) 
      { 
       if([[[dictAddress objectForKey:@"types"] objectAtIndex:0] isEqualToString:@"locality"]) 
       { 
        return [dictAddress objectForKey:@"long_name"]; 
       } 
       else if([[[dictAddress objectForKey:@"types"] objectAtIndex:0] isEqualToString:@"political"]) 
       { 
        return [dictAddress objectForKey:@"long_name"]; 
       } 
      } 
      else 
      { 
       return @""; 
      } 
     } 
     return @""; 
     } 
    } 
}]; 

使用這種方法,你會發現它與類型「本地」或「政治的」啓動區的名稱。

但我已經採取了第一個對象的API響應,但如果你需要比這個更大,然後把循環採取的JSON對象叫。

希望這會幫助你。

+0

感謝Chirag對我有用 – Sarao

+0

@Sarao happy coading –

1

您需要將JSON對象的字符串轉換爲NSDictionary,然後使用鍵/值訪問器來獲取每個字段。

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if (!error) 
    { 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
     NSArray *results = [json valueForKey:@"results"]; 
     for (NSDictionary *result in results) 
     { 
      NSLog(@"%@", [result valueForKey:@"formatted_address"]); 
      NSLog(@"%@", [result valueForKey:@"place_id"]); 
      // Etc... 
     } 
    } 
}]; 
1

首先,我會使用NSURLSessions和塊來進行網絡調用。塊的美妙之處在於,如果我的網絡連接不好,我的塊只會在調用返回後纔會執行。

然後,您需要將您的JSON轉換爲可以使用的基礎對象。

NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297,76.444017]; 
NSURL *url = [NSURL URLWithString:urlString]; 

//Next we need a NSURLSession instance 
NSURLSession *session = [NSURLSession sharedSession]; 

//All tasks (there are three, see documentation) are created from an NSURLSession instance 
//We want a DataTask 
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    //This "block" (of code) will be executed when the call is complete 

    //Serialize the JSON to Foundation objects 
    NSDictionary *geocodeDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

    //Parse through the JSON to get to where we want, address_components array 
    NSArray *resultsArray = geocodeDictionary[@"results"]; 
    NSDictionary *resultsDictionary = resultsArray[0]; 
    NSArray *addressComponents = resultsDictionary[@"address_components"]; 

    //Declare variables to hold desired results 
    NSString *longName; 
    NSString *shortName; 

    //The address_components array contains many dictionaries, 
    //we loop through each dictionary and check the types array 
    for (NSDictionary *addressComponentDictionary in addressComponents) { 
     NSArray *typesArray = addressComponentDictionary[@"types"]; 

     if ( [typesArray[0] isEqualToString:@"locality"] 
      && [typesArray[1] isEqualToString:@"political"]) { 
      //the type IS locality and political 
      longName = addressComponentDictionary[@"long_name"]; 
      shortName = addressComponentDictionary[@"short_name"]; 
     } 
    } 

    //Test log to see we are correct 
    NSLog(@"%@", longName); 
    NSLog(@"%@", shortName); 
}]; 

[dataTask resume];