我有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},

10-06 04:46