【问题标题】:Using Google maps with HTML geolocation使用带有 HTML 地理位置的 Google 地图
【发布时间】:2018-10-31 16:12:14
【问题描述】:

我正在使用地理位置来获取用户的当前位置。问题是有时它不会返回准确的位置。然后我想允许用户在谷歌地图上的正确位置手动设置标记,然后返回这个新位置的经度和纬度。

请问有什么想法吗?

这是我在使用地理位置返回用户位置时使用的代码

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

function showPosition(position) {
var latitude= position.coords.latitude;
var longitude= position.coords.longitude;
document.getElementById("location-x").innerHTML = "Latitude: " + latitude + 
"<br>Longitude: " + longitude;
$("#latitude").val(latitude);
$("#longitude").val(longitude);
 var uluru = {lat: latitude, lng: longitude};
 $("#map").show();
 var map = new google.maps.Map(
  document.getElementById('map'), {zoom: 15, center: uluru});

  var marker = new google.maps.Marker({position: uluru, map: map});
  }

【问题讨论】:

    标签: javascript html laravel google-maps


    【解决方案1】:

    我发现你可以设置“draggable:true”,

    var marker = new google.maps.Marker({position: uluru, map: map,draggable:true});
    

    并通过向标记添加事件侦听器来获取标记的当前位置

    google.maps.event.addListener(marker, 'dragend', function(evt){
    document.getElementById('location-x').innerHTML = 'Latitude: ' 
    + evt.latLng.lat().toFixed(3) + ' Longitude: ' + evt.latLng.lng().toFixed(3);
    });
    
     google.maps.event.addListener(marker, 'dragstart', function(evt){
    document.getElementById('location-x').innerHTML = 'Getting new location...';
    });
    

    我希望这对某人有所帮助。干杯

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 2018-07-30
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多