Bing Maps API request returning Uncaught SyntaxError: Unexpected token :
I'm getting an error Uncaught SyntaxError: Unexpected token : (dev.virtualearth.net/:1)
Here is my javascript.
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this 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 is valid but not sure where the error is occuring.
There are a few issues with your query:
Here is a code sample that lets you get the users location and 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>
链接地址: http://www.djcxy.com/p/8028.html