Javascript:Uncaught TypeError:无法读取属性' lat'未定义的(...)

时间:2016-11-03 18:52:07

标签: javascript jquery google-maps

我正在整合Google地图,用户会在其位置放置一个图钉。然后我使用dragend事件来获取lat和lng。我有以下代码,但我一直收到以下错误:

int current = 0;
private void Button1_Click(object sender, EventArgs e)
{
    current = bindingSource1.Position;
    tableAdapter1.Fill(dataSet1.Table1);
    bindingSource1.Position = Math.Min(current + 1, bindingSource1.Count - 1);
}

问题是什么?

 <style name="myTextStyle" parent="AppTheme">
     <item name="android:textColor">#FF0000</item>
     <item name="android:textSize">10sp</item>
 </style>

我也有:

Uncaught TypeError: Cannot read property 'lat' of undefined(…)

当然这应该有用吗?

2 个答案:

答案 0 :(得分:0)

JavaScript区分大小写,因此您需要以这种方式对待它 试试

document.getElementById("latbox").value = event.latLng.lat();
document.getElementById("lngbox").value = event.latLng.lng();

(注意l中的小写latLng

答案 1 :(得分:0)

MouseEvent没有.LatLng属性。 Javascript区分大小写,属性名称为.latLng

变化:

google.maps.event.addListener(marker, 'dragend', function (event) {
  document.getElementById("latbox").value = event.LatLng.lat();
  document.getElementById("lngbox").value = event.LatLng.lng();
});

要:

google.maps.event.addListener(marker, 'dragend', function (event) {
  document.getElementById("latbox").value = event.latLng.lat();
  document.getElementById("lngbox").value = event.latLng.lng();
});

proof of concept fiddle

代码段

&#13;
&#13;
function initialize() {
  var map;

  var latlng = new google.maps.LatLng(-17.86389, 31.02972);
  var myOptions = {
    zoom: 10,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

  // Creating a marker and positioning it on the map  
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(-17.86389, 31.02972),
    map: map,
    draggable: true,
    title: "Drag pin to your exact store location."
  });


  google.maps.event.addListener(marker, 'dragend', function(event) {
    document.getElementById("latbox").value = event.latLng.lat();
    document.getElementById("lngbox").value = event.latLng.lng();

  });

}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input size="20" type="text" id="latbox" name="lat">
<input size="20" type="text" id="lngbox" name="lng">
<div id="map_canvas" style="width:100%; height:100%"></div>
&#13;
&#13;
&#13;