可能是以前问过的类似问题。但是我正在寻找位置。当我尝试下面的代码就说明我的互联网服务提供商的位置。并且不显示我的当前位置。
function ipLookUp () {
$.ajax('http://ip-api.com/json')
.then(
function success(response) {
console.log('User\'s Location Data is ', response);
console.log('User\'s Country', response.country);
getAdress(response.lat, response.lon)
},
function fail(data, status) {
console.log('Request failed. Returned status of',
status);
}
);
}
function getAddress (latitude, longitude) {
$.ajax('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&key=***************************************')
.then(
function success (response) {
console.log('User\'s Address Data is ', response)
},
function fail (status) {
console.log('Request failed. Returned status of',
status)
}
)
}
if ("geolocation" in navigator) {
// check if geolocation is supported/enabled on current browser
navigator.geolocation.getCurrentPosition(
function success(position) { // for when getting location is a success
console.log('latitude', position.coords.latitude, 'longitude', position.coords.longitude);
getAddress(position.coords.latitude, position.coords.longitude);
},
function error(error_message) { // for when getting location results in an error
console.error('An error has occured while retrieving location', error_message);
ipLookUp();
}
);
} else {
// geolocation is not supported
// get your location some other way
console.log('geolocation is not enabled on this browser')
ipLookUp()
}
显示我的ISP城市名称。但我想这样的样本图片我目前的位置名称。 这是示例图像。并在google搜索结果页中显示我当前的城市和街道名称。但是当我使用上面的代码时,它显示如下。
AS55836 Reliance Jio Infocomm Limited"
city: "Coimbatore"
country: "India"
isp: "RJIL Tamilnadu LTE SUBSCRIBER PUBLIC"
org: "Reliance Jio infocomm ltd"
请教一些专家的意见,以准确地跟踪位置。
答案 0 :(得分:2)
这是在地图文档中使用的示例,此处不适用: https://developers.google.com/maps/documentation/javascript/geolocation
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</body>
</html>
我认为这可以解决您的问题。 ps:不适用于api_key的摘要