使用纬度和经度获取城市名称

时间:2020-06-11 05:26:50

标签: javascript google-maps

我正在尝试使用经度和纬度来获取城市名称。我使用代码获取了经纬度。

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}

在这里,我既有经度,又有经度。我正在使用Google Map API http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true&key=YOUR_KEY。但似乎我需要HTTPS。有什么办法可以与我本地主机上的HTTP一起使用

2 个答案:

答案 0 :(得分:0)

如文档中所述:

Best Practices Using Google Maps APIs Web Services

SSL访问

https://maps.googleapis.com/maps/api/service/output?parameters

所有包含用户数据或开发者标识符的Maps API Web服务请求都必须使用

HTTPS。通过HTTP发出的包含敏感数据的请求可能会被拒绝。

因此,您的问题的答案:

我可以通过本地主机的HTTP进行任何使用

这是不可能的,因为对API的HTTP请求很可能会被拒绝。

答案 1 :(得分:0)

我建议使用一些简单的方法通过ngrok之类的https访问localhost

https://nullcast.io/how-to-forward-localhost-to-any-device-for-testing-ngrok/