Get my current address using javascript(使用 javascript 获取我当前的地址)
本文介绍了使用 javascript 获取我当前的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对使用 Javascript 获取当前地址很感兴趣,只是通过组装其他一些 SO 线程来解决这个问题(1,2) 所以想发布这个问题和答案.
I was interested in getting my current address using Javascript and just figured this out by assembling some other SO threads (1,2) so wanted to post this question and answer.
请看下面的答案.
推荐答案
这是 HTML:
<script src="aHR0cDovL21hcHMuZ29vZ2xlLmNvbS9tYXBzL2FwaS9qcz9zZW5zb3I9ZmFsc2U="></script>
<p id='latitudeAndLongitude'></p>
<p id='address'></p>
这是 JS:
var latitudeAndLongitude=document.getElementById("latitudeAndLongitude"),
location={
latitude:'',
longitude:''
};
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
latitudeAndLongitude.innerHTML="Geolocation is not supported by this browser.";
}
function showPosition(position){
location.latitude=position.coords.latitude;
location.longitude=position.coords.longitude;
latitudeAndLongitude.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(location.latitude, location.longitude);
if (geocoder) {
geocoder.geocode({ 'latLng': latLng}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].formatted_address);
$('#address').html('Address:'+results[0].formatted_address);
}
else {
$('#address').html('Geocoding failed: '+status);
console.log("Geocoding failed: " + status);
}
}); //geocoder.geocode()
}
} //showPosition
这篇关于使用 javascript 获取我当前的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用 javascript 获取我当前的地址


猜你喜欢
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01