Bing Maps REST API - How to retrieve custom parameter
I'm trying to retrieve the value of the jsonso state object passed as a custom paramter into the Bing Maps REST Geo Location (http://dev.virtualearth.net/REST/v1/Locations/...). I can see the value returned as a second parameter in the response method c开发者_如何学运维all (Using network monitor), but can't seem to access it in code. Here's what the return method looks like (excerpted):
jsonp1301929429652({"authenticationResultCode":"ValidCredentials", ...more data... ,"traceId":"41a7523951964ae9a76ca4ea91c4e80f|CH1M001467|02.00.82.2800|CH1MSNVM001383"}, "My Custom Value")
I'm trying to pick it up in the anonymous method, like this:
$.getJSON(geocodeRequest, function (d, customParam) {
if (d != null && d.resourceSets.length > 0) {
var coordinates = d.resourceSets[0].resources[0].point.coordinates;
var loc = new Microsoft.Maps.Location(coordinates[0], coordinates[1]);
....
}
}
Unfortunately, the customParam is always undefined. How can I get that value?
Okay, here is a testable example:
$(function () {
var mapOptions = {
credentials: "Your API Key"
}
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/US/Your State Abbrev/Your Zip/Your City/Your Street Address?jsonso=ABC123&jsonp=?&key=" + mapOptions.credentials;
$.getJSON(geocodeRequest, function (d, arg1, arg2, arg3) {
if (d != null && d.resourceSets.length > 0) {
var readMyCustomParam = arg1;
}
});
});
If you break on the first line inside the getJSON request you can monitor the traffic you will see the result of the request returns the jsonso parameter properly. My problem is then reading that in javascript. The whole purpose for this is to loop through a list of dealers, async geo locate, then use the returned identifier to lookup the dealer in the array to load it's description, etc for the pushpoint mouseover.
@Corgalore, sorry for the delay, I probably should have registered with an email I use more frequently. In case this has not yet been solved.. I see your value is coming back, but it's not well formed
per JSLint.com :
Error:
Problem at line 1 character 568: Expected '(end)' and instead saw ','.
{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http:\/\/dev.v...
JSON: bad.
{"authenticationResultCode":"ValidCredentials",
"brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"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.",
"resourceSets":[{"estimatedTotal":0,
"resources":[]}],
"statusCode":200,
"statusDescription":"OK",
"traceId":""},
"ABC123"
Remove the , "ABC123" and JSLint says "JSON: good."
It appears you'll have to use a custom parser to get that value ...or at least a parser that's more forgiving. Unless I was doing something wrong, jsonParse using jQuery didn't work for me on this string.
Unless you're using a custom parsing method, it appears you're not returning valid JSON (http://www.jsonlint.com/). It's also important to know if that custom value is a DateTime as this would require special handling in .NET.
There's a post here that shows how you can grab any errors that may be occurring: $("#msg").ajaxError...
but you may want to try using a key value pair, something like:
$.getJSON(geocoderequest, function(data) {
$.each(data, function(key,val) {
// etc...
精彩评论