javascript - 谷歌地图获取可以通过拖动图钉更改的当前位置

标签 javascript html google-maps google-maps-api-3 geolocation

这是我的第一篇文章,如果之前有人问过,我很抱歉。

我有一个用户在移动设备上填写的表单,该表单使用 JavaScript 地理定位捕获经度和纬度。这并不总是最准确的,所以我尝试结合使用地理定位和谷歌地图可拖动图钉。

所以用户打开页面,地理定位在下面的脚本中输入当前的经度和纬度,如果错误,他们可以将图钉拖到正确的位置,从而更新文本字段。

我已经尝试了所有方法,但结果好坏参半...如果有人能帮助我,那就太好了。

我希望这是有道理的......如果不是我很乐意解释更多

下面的脚本使您能够拖动图钉并使用经度和纬度更新两个文本字段。

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
  <input type="text" id="latitude" placeholder="latitude">
  <input type="text" id="longitude" placeholder="longitude">
  <div id="map" style="width:500px; height:500px"></div>
  
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
  <script>
	function initialize() {
		var $latitude = document.getElementById('latitude');
		var $longitude = document.getElementById('longitude');
		var latitude = 50.715591133433854
		var longitude = -3.53485107421875;
		var zoom = 7;
		
		var LatLng = new google.maps.LatLng(latitude, longitude);
		
		var mapOptions = {
			zoom: zoom,
			center: LatLng,
			panControl: false,
			zoomControl: false,
			scaleControl: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}	
		
		var map = new google.maps.Map(document.getElementById('map'),mapOptions);
      
		
		var marker = new google.maps.Marker({
			position: LatLng,
			map: map,
			title: 'Drag Me!',
			draggable: true
		});
		
		google.maps.event.addListener(marker, 'dragend', function(marker){
			var latLng = marker.latLng;
			$latitude.value = latLng.lat();
			$longitude.value = latLng.lng();
		});
		
		
	}
	initialize();
	</script>
  
</body>
</html>

最佳答案

<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
    function initialize() {
      var map;
      var position = new google.maps.LatLng(50.45, 4.45);    // set your own default location.
      var myOptions = {
        zoom: 15,
        center: position
      };
      var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

      // We send a request to search for the location of the user.  
      // If that location is found, we will zoom/pan to this place, and set a marker
      navigator.geolocation.getCurrentPosition(locationFound, locationNotFound);

      function locationFound(position) {
        // we will zoom/pan to this place, and set a marker
        var location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        // var accuracy = position.coords.accuracy;

        map.setCenter(location);
        var marker = new google.maps.Marker({
            position: location, 
            map: map, 
            draggable: true,
            title: "You are here! Drag the marker to the exact location."
        });
        // set the value an value of the <input>
        updateInput(location.lat(), location.lng());

        // Add a "drag end" event handler
        google.maps.event.addListener(marker, 'dragend', function() {
          updateInput(this.position.lat(), this.position.lng());
        });


      }

      function locationNotFound() {
        // location not found, you might want to do something here
      }

    }
    function updateInput(lat, lng) {
      document.getElementById("my_location").value = lat + ',' + lng;
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#map-canvas {
  width: 500px;
  height: 400px;
}
</style>
<div id="map-canvas"></div>
Location:<br>
<input id="my_location" readonly="readonly">

关于javascript - 谷歌地图获取可以通过拖动图钉更改的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26887416/

相关文章:

javascript - 水平和垂直滚动 HTML 表格,同时左列固定,标题固定以进行垂直滚动

javascript - 谷歌地图 API v3 : is there any function to check if a coordinate is inside a province or country

javascript - 当鼠标光标纬度等于标记纬度时发出警报消息(Google map API v3)

javascript - 在 Nodejs 中编辑所需/导出的变量

javascript - Typescript:使用 fromJson 方法指定对象,如何测试私有(private)属性是否存在并设置它

javascript - amcharts hideGraph 与 graphs.hidden

javascript - 显示动态更新的 HTML 输入值时出现问题

javascript - 在 `.ajax`之外定义一个json解析函数

html - 用于响应式布局的标记和 CSS, float div 与顶部对齐

c# - 如何在ASP.net网页的服务器端使用文本文件读写?