0

I am trying to parse out a Reverse Geo-location using Bing Maps.

http://www.microsoft.com/maps/isdk/ajax/ Find Information > Reverse Find

If you look at the code, when you look up an address, you get this back

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');
}

The code I currently use parses the "Name" into it's sections so that I can use it elsewhere.

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;
        }
    }

I want to change the above code to use the section that has the addressline, location, postaltown, etc already.

3 Answers 3

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;
        }
    }

That'll do what you want. But it's not very nice practise. First of all - if you have multiple locations, each will overwrite the other. Second, this pollutes the window namespace which is not recommended.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you calling it like GetResults(_f1300044038369())? If so, you have to do GetResults(_f1300044038369().Results). If you do that, it isn't undefined -- see this fiddle if you must jsfiddle.net/idbentley/G2Pkk
I was calling it just like Microsoft has it in the link above. microsoft.com/maps/isdk/ajax Find Information > Reverse Find
Alright, well in that case you'll have to change the code a little bit - as GetResults is used as a call-back with the full JSON object. I've edited the above answer to support this.
0

It looks like you are already passing the "Results" object into the function as the "locations" argument, so I will work under that assumption. Instead of referencing locations[i].Name, you can reference locations[i].Address. This will give you an object that should have all the properties you need.

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...
    }
  }

}

1 Comment

What is null? do a console.log(locations); to see if what you are passing in is undefined, also to check the object tree that gets output to the log to inspect the actual property names
0

The only thing you need to do is change your loc_array lines:

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;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.