在mvc剃刀中整合谷歌地图与标记

时间:2017-09-29 17:53:37

标签: javascript asp.net-mvc google-maps razor google-maps-api-3

我正在做一个项目,我需要整合谷歌地图。我已经给了谷歌地图,并给了两个纬度和经度的文本框。目前,在运行应用程序时,Google地图和标记可见。但我的问题是,当点击或拖动标记时,在文本框中看不到更改的纬度和经度。并显示未定义标记的java脚本错误。

视图

<div class="col-lg-7" id="dvMap" style="height:250px;"> </div>

<div class="form-group">
        <div class="col-lg-9 col-md-8">
            @Html.EditorFor(model => model.Lat, new { htmlAttributes = new { @class = "form-control", @placeholder = "Latitude" } })
            @Html.ValidationMessageFor(model => model.Lat, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-lg-9 col-md-8">
            @Html.EditorFor(model => model.Long, new { htmlAttributes = new { @class = "form-control", @placeholder = "Longitude" } })
            @Html.ValidationMessageFor(model => model.Long, "", new { @class = "text-danger" })
        </div>
    </div>

</div>


<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=MY_API_KEY"></script>

<script type="text/javascript">
        window.onload = function () {
    //map..
    var map = new google.maps.Map(document.getElementById('dvMap'), {
        center: {
            lat: 10.9968683,
            lng: 76.00882
        },
        zoom: 15
    });
    //marker..
    var marker = new google.maps.Marker({
        position: {
            lat: 10.9968683,
            lng: 76.00882
        },
        map: map,
        draggable: true
    });
}
//dragend event of marker
google.maps.event.addListener(marker,'dragend', function () {
    var Lats = marker.getPosition().lat();
    var Longs = marker.getPosition().lng();

    $('#Lat').val(Lats);
    $('#Long').val(Longs);
});

我该如何解决问题?谁能帮我找一个解决方案?

1 个答案:

答案 0 :(得分:0)

我重新创建了您的设置,以下是您可以执行的一些操作:

调用Google Maps Javascript API时添加回调参数。请不要在此处或任何公开区域发布您的API密钥,任何人都可以滥用它。

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=YOUR_API_KEY&callback=initMap"

将window.onload更改为指定的回调函数initMap,并在那里包含您的侦听器。这样,当我们获取Google Maps Javascript API时,它会在确保您的Maps对象未定义之后立即调用initMap()

function initMap() {
//map..
var map = new google.maps.Map(document.getElementById('dvMap'), {
    center: {
        lat: 10.9968683,
        lng: 76.00882
    },
    zoom: 15
});
//marker..
var marker = new google.maps.Marker({
    position: {
        lat: 10.9968683,
        lng: 76.00882
    },
    map: map,
    draggable: true
});

//dragend event of marker
google.maps.event.addListener(marker,'dragend', function () {
  var Lats = marker.getPosition().lat();
  var Longs = marker.getPosition().lng();
  $('#Lat').val(Lats);
  $('#Long').val(Longs);
 });
}

这是一个不使用Razor语法的工作示例,因此您可以看到它单独工作:

      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
  </head>
  <body>
    <div id="dvMap" style="height:250px;"></div>
		<input type="text" id="Lat">
		<input type="text" id="Long">
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyAZVylT7o5OxdosVfh-IVONHoaA0cpN5VI&callback=initMap" async defer></script>
<script type="text/javascript">
  function initMap() {
    //map..
    var map = new google.maps.Map(document.getElementById('dvMap'), {
        center: {
            lat: 10.9968683,
            lng: 76.00882
        },
        zoom: 15
    });
    //marker..
    var marker = new google.maps.Marker({
        position: {
            lat: 10.9968683,
            lng: 76.00882
        },
        map: map,
        draggable: true
    });
		
			//dragend event of marker
google.maps.event.addListener(marker,'dragend', function () {
    var Lats = marker.getPosition().lat();
    var Longs = marker.getPosition().lng();

    $('#Lat').val(Lats);
    $('#Long').val(Longs);

});
}
		</script>
  </body>
</html>

这是一个外部链接,显示它在.NETFiddle中如何工作忽略控制器和模型。

希望这有帮助!