2015-02-24 77 views
0

我使用谷歌地圖的地方自動完成API自動完成用戶輸入地址,一旦我與所需的地址的選擇完成後,通過選擇地址字符串從自動完成地址到谷歌地圖Google地方geocode api,以獲得詳細的組件,問題是有時地理編碼API即使自動完成API返回相同的地址有它不返回街道號,不知道它出錯的地方,下面是我使用的代碼和來自服務谷歌地圖上的地址自動完成和地理編碼API返回的diff地址

這是我如何從地理編碼服務獲取詳細地址

NSString* address = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",completionText]; 
    NSString* str = [address stringByReplacingOccurrencesOfString:@"," 
                 withString:@""]; 
    str = [str stringByReplacingOccurrencesOfString:@" " 
             withString:@"+"]; 


    NSURL *requestURL = [NSURL URLWithString:str]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)]; 

    //response 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSError *jsonParsingError = nil; 
    NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; 

    NSLog(@"%@",locationResults); 
    NSDictionary* results = [[locationResults objectForKey:@"results"] objectAtIndex:0]; 
    NSArray* address_components = [results objectForKey:@"address_components"]; 


    for (id addr in address_components) { 
     NSString *long_name = [addr valueForKey:@"long_name"]; 
     NSString *short_name = [addr valueForKey:@"short_name"]; 
     NSString *types = [[addr valueForKey:@"types"] objectAtIndex:0]; 


     if ([types rangeOfString:@"street_number"].location != NSNotFound) 
     { 
      streetNum = long_name; 
     } 
     else if ([types rangeOfString:@"route"].location != NSNotFound) 
     { 
      streetName = long_name; 
     } 
     else if ([types rangeOfString:@"locality"].location != NSNotFound) 
     { 
      suburb = long_name; 
     } 
     else if ([types rangeOfString:@"administrative_area_level_1"].location != NSNotFound) 
     { 
      state = short_name; 
     } 
     else if ([types rangeOfString:@"country"].location != NSNotFound) 
     { 
      country = short_name; 
     } 
     else if ([types rangeOfString:@"postal_code"].location != NSNotFound) 
     { 
      postalCode = short_name; 
     } 

這裏是地址有一個地址的例子門牌號碼,但地理編碼點返回它

{ 
     "description" : "740 Spring Lane, Wallacedale, Victoria, Australia", 
     "id" : "ad2c41d2f934a30f4c5f8c4b336df8de988174d6", 
     "matched_substrings" : [ 
      { 
       "length" : 10, 
       "offset" : 0 
      }, 
      { 
       "length" : 8, 
       "offset" : 30 
      }, 
      { 
       "length" : 9, 
       "offset" : 40 
      } 
     ], 
     "place_id" : "EjE3NDAgU3ByaW5nIExhbmUsIFdhbGxhY2VkYWxlLCBWaWN0b3JpYSwgQXVzdHJhbGlh", 
     "reference" : "CkQ1AAAASPpKC9JU4XQoFf3LUYwoF_hJFRpFt5VE67NhCPs8vV9ghXvVUeKgPm-BH-Owx-zvuhHEPhJU-Y2ReK5S49AdphIQiKpvir0TegpxEAjmmNJEJhoUCt7KXyZ6LoN51qT50Unrl5ceYMU", 
     "terms" : [ 
      { 
       "offset" : 0, 
       "value" : "740 Spring Lane" 
      }, 
      { 
       "offset" : 17, 
       "value" : "Wallacedale" 
      }, 
      { 
       "offset" : 30, 
       "value" : "Victoria" 
      }, 
      { 
       "offset" : 40, 
       "value" : "Australia" 
      } 
     ], 
     "types" : [ "route", "geocode" ] 
     } 

以下是谷歌地圖的地理編碼API輸出爲上述自動完成地址

{ 
    results =  (
       { 
      "address_components" =    (
           { 
        "long_name" = "Spring Lane"; 
        "short_name" = "Spring Ln"; 
        types =      (
         route 
        ); 
       }, 
           { 
        "long_name" = Wallacedale; 
        "short_name" = Wallacedale; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = Victoria; 
        "short_name" = VIC; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Australia; 
        "short_name" = AU; 
        types =      (
         country, 
         political 
        ); 
       }, 
           { 
        "long_name" = 3303; 
        "short_name" = 3303; 
        types =      (
         "postal_code" 
        ); 
       } 
      ); 
      "formatted_address" = "Spring Lane, Wallacedale VIC 3303, Australia"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "-37.9132082"; 
         lng = "141.8506572"; 
        }; 
        southwest =      { 
         lat = "-37.9283224"; 
         lng = "141.8505337"; 
        }; 
       }; 
       location =     { 
        lat = "-37.9266631"; 
        lng = "141.8506572"; 
       }; 
       "location_type" = "GEOMETRIC_CENTER"; 
       viewport =     { 
        northeast =      { 
         lat = "-37.9132082"; 
         lng = "141.8519444302915"; 
        }; 
        southwest =      { 
         lat = "-37.9283224"; 
         lng = "141.8492464697085"; 
        }; 
       }; 
      }; 
      "partial_match" = 1; 
      "place_id" = "ChIJV74lHNPCzGoRnS8t_EDR8zk"; 
      types =    (
       route 
      ); 
     } 
    ); 
    status = OK; 
} 

輸出行的事具有組件類型「街道號」,其中自動完成的地址字符串在開始時正確。這發生在許多地址,什麼可能是錯誤的?還是我需要使用其他服務?其具有街道號碼的地址的 實施例是下面

這是通過自動完成API返回

{ 
     "description" : "11 Victoria Parade, Collingwood, Victoria, Australia", 
     "id" : "dbeebdb81c8babd166ac8f89878e88c2cba333a1", 
     "matched_substrings" : [ 
      { 
       "length" : 18, 
       "offset" : 0 
      }, 
      { 
       "length" : 9, 
       "offset" : 43 
      } 
     ], 
     "place_id" : "EjQxMSBWaWN0b3JpYSBQYXJhZGUsIENvbGxpbmd3b29kLCBWaWN0b3JpYSwgQXVzdHJhbGlh", 
     "reference" : "CkQ4AAAATy4Z7xc3ermqeWnlxpl81pAVsBWOIl3S2JbCwwRxraLt39OJauWFnCR3NgGyruVBXfUo236mMe6CCQt5XRSYwRIQEL8JC7Qo8j3-0iD4AnJTtBoURjzgm8aHBa2g0tY4lkTKYTuVGPg", 
     "terms" : [ 
      { 
       "offset" : 0, 
       "value" : "11 Victoria Parade" 
      }, 
      { 
       "offset" : 20, 
       "value" : "Collingwood" 
      }, 
      { 
       "offset" : 33, 
       "value" : "Victoria" 
      }, 
      { 
       "offset" : 43, 
       "value" : "Australia" 
      } 
     ], 
     "types" : [ "route", "geocode" ] 
     } 

並且在下面發送自動完成的地址作爲輸入

results =  (
       { 
      "address_components" =    (
           { 
        "long_name" = 11; 
        "short_name" = 11; 
        types =      (
         "street_number" 
        ); 
       }, 
           { 
        "long_name" = "Victoria Parade"; 
        "short_name" = "Victoria Parade"; 
        types =      (
         route 
        ); 
       }, 
           { 
        "long_name" = Collingwood; 
        "short_name" = Collingwood; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = Victoria; 
        "short_name" = VIC; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Australia; 
        "short_name" = AU; 
        types =      (
         country, 
         political 
        ); 
       }, 
           { 
        "long_name" = 3066; 
        "short_name" = 3066; 
        types =      (
         "postal_code" 
        ); 
       } 
      ); 
      "formatted_address" = "11 Victoria Parade, Collingwood VIC 3066, Australia"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "-37.8087633"; 
         lng = "144.9830508"; 
        }; 
        southwest =      { 
         lat = "-37.8087778"; 
         lng = "144.9830487"; 
        }; 
       }; 
       location =     { 
        lat = "-37.8087633"; 
        lng = "144.9830508"; 
       }; 
       "location_type" = "RANGE_INTERPOLATED"; 
       viewport =     { 
        northeast =      { 
         lat = "-37.8074215697085"; 
         lng = "144.9843987302915"; 
        }; 
        southwest =      { 
         lat = "-37.8101195302915"; 
         lng = "144.9817007697085"; 
        }; 
       }; 
      }; 
      "place_id" = EjMxMSBWaWN0b3JpYSBQYXJhZGUsIENvbGxpbmd3b29kIFZJQyAzMDY2LCBBdXN0cmFsaWE; 
      types =    (
       "street_address" 
      ); 
     } 
    ); 
    status = OK; 
} 
後由地理編碼服務返回的結果

任何幫助將不勝感激。

回答

2

您在由自動完成,結果說明得到的是隻是一個字符串,僅此而已(不是一套類似街,拉鍊或housenumber功能)。服務嘗試找到確切位置(基於輸入),但匹配可能是部分。

當你類型740弄春,Wallacedale,維多利亞,澳大利亞http://maps.google.com,你會得到一個結果爲春LN,Wallacedale VIC 3303,澳大利亞

也可以鍵入12345春天裏,Wallacedale ,澳大利亞維多利亞,你會得到同樣的結果。

除此之外:我會基礎上,place_id建議request the place-details。雖然它不能給你想要的結果(一個房號),但結果應該更精確(綁定到特定的地方而不是地點)。它也將返回該地點的更多細節。

相關問題