繁体   English   中英

如何从经纬度中获取价值以在脚本中创建Google Maps标记?

[英]How to get value from latitude and longitude to create google maps marker in script?

我有一个HTML输入文本,需要传递坐标才能在Google地图中做一个marker

jsfiddle.net/#&togetherjs=r3M9Kp7ff7

如何将其从HTML传递到具有良好数据类型的JavaScript?

<label for="latitude">Latitude</label>
<input id="latitude" type="text" ng-model="ctrl.department.latitude" class="form-control" required="" placeholder="Latitude" value=""/>
<label for="longitude">Longitude</label>
<input id="longitude" type="text" ng-model="ctrl.department.longitude" class="form-control" required="" placeholder="Longitude" value=""/>
<label for="map">Pozycja na mapie</label>
<div id="map">
<script>
function initMap() {
  var myLat = document.getElementById('latitude').value;
  var myLng = document.getElementById('longitude').value;
  var map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: 51.75, lng: 19.46},
                zoom: 10,
                mapTypeId: 'roadmap'
});

var marker = new google.maps.Marker({
                 position:{myLat, myLng}, // not working
                 map:map,
});
</script>

在脚本中尝试这个

 <script>
      function initMap() {
        var myLat = document.getElementById('latitude').value;
        var myLng = document.getElementById('longitude').value;

        var myCenter = {lat: myLat, lng: myLng};

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: myCenter
        });

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

      }
    </script>

标记位置的语法应为
位置:{lat:myLat,lng:myLng},而不是此位置:{myLat,myLng},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM