我收到错误Uncaught SyntaxError:意外的令牌:(dev.virtualearth.net /:1)
这是我的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());
});
}
返回的json有效,但不确定错误发生的位置。
答案 0 :(得分:1)
您的查询存在一些问题:
以下是一个代码示例,可让您获取用户位置并对其进行反向地理编码:
<!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>