2011-03-14 93 views
0

我想解析出使用Bing地图的反向地理位置。JSON解析Bing地图响应

http://www.microsoft.com/maps/isdk/ajax/ 查找信息>反向查找

如果你在看代码,当你查找地址,你把它恢复

function _f1300044038369() { 
    return { 
     "d": { 
      "__type": "Microsoft.VirtualEarth.Engines.Core.Geocoding.ReverseGeocodeResponse", 
      "Results": [{ 
       "Name": "SW 35th Ave, Tualatin, OR 97062", 
       "Type": 0, 
       "BestLocation": { 
        "Precision": 0, 
        "Coordinates": { 
         "Latitude": 45.378872752189636, 
         "Longitude": -122.71288096904755 
        } 
       }, 
       "Locations": [{ 
        "Precision": 0, 
        "Coordinates": { 
         "Latitude": 45.378872752189636, 
         "Longitude": -122.71288096904755 
        } 
       }], 
       "BestView": { 
        "NorthEastCorner": { 
         "Latitude": 45.382735469760313, 
         "Longitude": -122.70554921472814 
        }, 
        "SouthWestCorner": { 
         "Latitude": 45.37501003461896, 
         "Longitude": -122.72021272336696 
        }, 
        "Type": 0, 
        "Center": { 
         "Latitude": 45.378872884129805, 
         "Longitude": -122.71288096904755 
        } 
       }, 
       "Shape": null, 
       "Address": { 
        "AddressLine": "SW 35th Ave", 
        "Locality": "Tualatin", 
        "PostalTown": "", 
        "District": "", 
        "AdminDistrict": "OR", 
        "PostalCode": "97062", 
        "CountryRegion": "United States", 
        "FormattedAddress": "SW 35th Ave, Tualatin, OR 97062" 
       }, 
       "CountryRegion": 244, 
       "MatchConfidence": 1, 
       "MatchCode": 1 
      }], 
      "ResponseSummary": { 
       "Copyright": "Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.", 
       "StatusCode": 0, 
       "AuthResultCode": 0, 
       "ErrorMessage": null, 
       "TraceId": "dc1c3b20-6345-484c-9662-4df504d8977e|SN1M001054" 
      } 
     } 
    }.d; 
} 
if (typeof closeDependency !== 'undefined') { 
    closeDependency('1300044038369'); 
} 

我目前使用的代码解析“名称”进入它的部分,以便我可以在其他地方使用它。

function GetResults(locations) { 
    if (locations) { 
     for (var i = 0; i < locations.length; i++) { 
      s = locations[i].Name; 
      // 
      var addressSplit = s.split(", "); 
      addresscode = addressSplit[0] 
      citycode = addressSplit[1] 
      statezip = addressSplit[2] 
      country = addressSplit[3] 
      var statezipSplit = statezip.split(" "); 
      statecode = statezipSplit[0]; 
      zipcode = statezipSplit[1]; 
      var loc_array = new Array(); 
      loc_array[0] = addresscode; 
      loc_array[1] = citycode; 
      loc_array[2] = statecode; 
      loc_array[3] = zipcode; 
      window.locationArray = loc_array; 
     } 
    } 

我想更改上面的代码,以使用具有地址线,位置,postaltown等已经部分。

回答

4
function GetResults(locations) { 
    var locations = locations.Results; 
    if (locations) { 
     for (var i = 0; i < locations.length; i++) { 
      var addr = locations[i].Address, 
       loc_array = new Array() 
       addresscode, citycode, country, statecode, zipcode; 
      // 
      addresscode = addr.AddressLine; 
      citycode = addr.Locality; 
      country = addr.CountryRegion; 
      statecode =addr.AdminDistrict; 
      zipcode = addr.PostalCode; 
      loc_array[0] = addresscode; 
      loc_array[1] = citycode; 
      loc_array[2] = statecode; 
      loc_array[3] = zipcode; 
      window.locationArray = loc_array; 
     } 
    } 

这会做你想做的。但这不是很好的做法。首先 - 如果你有多个地点,每个地点都会覆盖另一个地点。其次,这会污染不推荐的窗口命名空间。

+0

获取空值 – BlindingDawn 2011-03-14 20:33:30

+0

你叫它像'GetResults(_f1300044038369())'?如果是这样,你必须做'GetResults(_f1300044038369()。Results)'。如果你这样做,它不是未定义 - 看到这个小提琴,如果你必须http://jsfiddle.net/idbentley/G2Pkk/ – idbentley 2011-03-14 22:12:35

+0

我称它就像微软有它在上面的链接。 http://www.microsoft.com/maps/isdk/ajax/查找信息>反向查找 – BlindingDawn 2011-03-15 01:36:20

0

看起来您已经将“Results”对象作为“locations”参数传递给函数,所以我将在该假设下工作。您可以参考位置[i] .Address,而不是参考位置[i] .Name。这会给你一个应该拥有你需要的所有属性的对象。

function GetResults(locations) { 
    if (locations) { 
    for (var i = 0; i < locations.length; i++) { 
     var s = locations[i].Address; 
     // 
     var address = s.AddressLine; 
     var city = s.Locality; 
     var state = s.AdminDistrict; 
     var zip = s.PostalCode; 
     var country = s.CountryRegion 

     // and so on... 
    } 
    } 

}

+0

获得空值 – BlindingDawn 2011-03-14 20:34:30

+0

什么是空?做一个console.log(位置);看看你传递的是未定义的,还要检查输出到日志的对象树来检查实际的属性名称 – 2011-03-18 19:46:58

0

你需要做的唯一的事情就是改变你的loc_array线:

function GetResults(locations) { 
    var s, location; 
    if (locations) { 
     for (var i = 0; i < locations.length; i++) { 
      s = locations[i].Name; 
      location = locations[i]; 
      // 
      var loc_array = []; 
      loc_array[0] = location.Address.AddressLine; 
      loc_array[1] = location.Address.Locality; 
      loc_array[2] = location.Address.AdminDistrict; 
      loc_array[3] = location.Locations.Coordinates.Latitude; 
      loc_array[4] = location.Locations.Coordinates.Longitude; 
      // ... 
      window.locationArray = loc_array; 
     } 
    } 
}