开发者

Bing maps, how to make geocode request without call back in 7 version?

I know how to make bing geocode request wi开发者_如何学Pythonth callback function, like this:

function MakeGeocodeRequest(credentials)
         {
            var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + document.getElementById('txtQuery').value + "?output=json&jsonp=GeocodeCallback&key=" + credentials;

            CallRestService(geocodeRequest);
         }

         function CallRestService(request) 
         {
            var script = document.createElement("script");
            script.setAttribute("type", "text/javascript");
            script.setAttribute("src", request);
            document.body.appendChild(script);
         }
         function GeocodeCallback(result) 
         {   
            // Do something with the result
         }

(Copied from msdn Maps AJAX Control 7.0 ISDK)

In Bing Map 6.2 version it was opportunity to make such request by using the next code:

map.Find(null, tempDest, null, null, null, null, null, null, null, null,
                                function (a, b, c, d, e) {
...
});

It was very useful because all variables were defined and ready to use, but in new version all my variables are undefined and I do not want do them as global, so do you know any solution how to make request without such callback?


Looks like version 7 doesn't support the 6.x methods. Here's an example for 7. Example is from http://www.bingmapsportal.com/isdk/ajaxv7#SearchModule2

Just added the alert(topResult.location); line so you see the location info.

function geocodeRequest() 
{ 
    createSearchManager(); 
    var where = 'Denver, CO'; 
    var userData = { name: 'Maps Test User', id: 'XYZ' }; 
    var request = 
    { 
        where: where, 
        count: 5, 
        bounds: map.getBounds(), 
        callback: onGeocodeSuccess, 
        errorCallback: onGeocodeFailed, 
        userData: userData 
    }; 
    searchManager.geocode(request); 
} 
function onGeocodeSuccess(result, userData) 
{ 

if (result) { 

        map.entities.clear(); 
        var topResult = result.results && result.results[0]; 
        if (topResult) { 
            alert(topResult.location);
            var pushpin = new Microsoft.Maps.Pushpin(topResult.location, null); 
            map.setView({ center: topResult.location, zoom: 10 }); 
            map.entities.push(pushpin); 
        } 
    } 
} 
function onGeocodeFailed(result, userData) { 
    displayAlert('Geocode failed'); 
} 

if (searchManager) 
{ 
    geocodeRequest(); 
} 
else 
{ 
    Microsoft.Maps.loadModule('Microsoft.Maps.Search', { callback: geocodeRequest }); 
}


Specify in url jsonso parameter. Example: var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + document.getElementById('txtQuery').value + "?jsonso=paramValue&output=json&jsonp=GeocodeCallback&key=" + credentials;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜