javascript - Bing Maps API request returning Uncaught SyntaxError: Unexpected token : -



javascript - Bing Maps API request returning Uncaught SyntaxError: Unexpected token : -

i'm getting error uncaught syntaxerror: unexpected token : (dev.virtualearth.net/:1)

here javascript.

function getlocation() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(showposition); } else { alert("geolocation not supported browswer or not enabled."); } } function showposition(position) { $.ajax({ url: 'http://dev.virtualearth.net/rest/v1/locations/', datatype: 'jsonp', data: { q: position.coords.latitude + ',' + position.coords.longitude, key: '***' } }).then(function(data) { //alert("latitude: " + position.coords.latitude + "<br /> longitude: " + position.coords.longitude + //"<br /> province: " + data.resourcesets.resources[0].address.locality); console.log(data.tostring()); }); }

the json returned valid not sure error occuring.

there few issues query:

coordinates should not passed query parameter doing it. not reverse geocode location think trying do. coordinates should part of url .../location/latitude,longitude?key= also set jsonp parameter of ajax method in add-on datatype. instead of using navigator, utilize geolocationprovider class part of bing maps api. navigator class not supported on older browsers, however, older browsers have other means of getting users location. geolocationprovider class wraps these different methods you. gives alternative show accuracy circle , set map view if want. use session key rest services instead of bing maps key. create service request non-billable transaction.

here code sample lets users location , reverse geocodes it:

<!doctype html> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script> <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript"> var map, sessionkey; function getmap() { map = new microsoft.maps.map(document.getelementbyid("mymap"), { credentials: "your_bing_maps_key" }); map.getcredentials(function(c){ sessionkey = c; }); } function getlocation() { var geolocationprovider = new microsoft.maps.geolocationprovider(map); geolocationprovider.getcurrentposition({ showaccuracycirle: false, updatemapview: false, successcallback: showposition }); } function showposition(position) { $.ajax({ url: 'http://dev.virtualearth.net/rest/v1/locations/' + position.center.latitude + ',' + position.center.longitude, datatype: "jsonp", jsonp: "jsonp", data: { key:sessionkey }, success: function (result) { if (result && result.resourcesets && result.resourcesets.length > 0 && result.resourcesets[0].resources && result.resourcesets[0].resources.length > 0) { alert(result.resourcesets[0].resources[0].address.formattedaddress); }else{ alert("no results found"); } }, error: function (e) { alert(e.statustext); } }); } </script> </head> <body onload="getmap();"> <div id='mymap' style="position:relative;with:800px;height:600px;"></div><br/> <input type="button" onclick="getlocation()" value="test"/> </body> </html>

javascript jquery bing-maps bing-maps-api

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -