javascript - 谷歌地图标记未定义

标签 javascript jquery google-maps google-maps-api-3

这是我的代码

var map_canvas = document.getElementById('map_canvas');
var map_options = { center: new google.maps.LatLng(44.5403, -78.5463), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP }
map = new google.maps.Map(map_canvas, map_options);


 google.maps.event.addListener(map, 'click', function(event) {
     if(marker){
         marker.setMap(null);
     }
     marker = new google.maps.Marker({
         position: event.latLng,
         map: map
     });
     $("#latitude").val(event.latLng.lat());
     $("#longitude").val(event.latLng.lng());
     map.setCenter(event.latlng);
 });

 console.log($("#longitude").val());
 if (($("#latitude").val()) && ($("#longitude").val())){
     var point = new google.maps.LatLng(($("#latitude").val()), ($("#longitude").val()));
     marker = new google.maps.marker({
         position: point,
         map: map
     });
     map.setCenter(point, 8);
 }

请注意,我已经添加了一个监听器,用于在 map 上单击以添加标记,并且它正在工作。我的意思是,当我单击 map 时,就会出现标记。

但是,当我提交页面时,如果输入中有错误,我会将页面返回给用户。在这种情况下,如果用户添加了 map ,我想为他创建 map 。这就是我使用这段代码的原因:

console.log($("#longitude").val());
if (($("#latitude").val()) && ($("#longitude").val())){
 var point = new google.maps.LatLng(($("#latitude").val()), ($("#longitude").val()));
 marker = new google.maps.marker({
     position: point,
     map: map
 });
 map.setCenter(point, 8);

但是,我在 marker = new google.maps.marker({ 你能帮忙吗? p>

最佳答案

确保 Google Maps API 函数名称使用正确的大小写,google.maps.marker 应为 google.maps.Marker

关于javascript - 谷歌地图标记未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24513209/

相关文章:

javascript - 在 Bootstrap 模式中实现无限滚动?

serialization - jquery ajax序列化问题

javascript - 显示消息

javascript - 如何根据表格单元格中的数据向表格行添加背景颜色?

javascript - jquery从选定的组合框获取数据属性

javascript - Uncaught ReferenceError : Oncheckboxclicked is not defined at HTMLElement. onclick

java - Android:Google Maps v2 API 在 Android 4.0 及更高版本上加载空白 map

javascript - 在谷歌地图上设置标记

javascript - jQuery 单击功能不适用于 ID

google-maps - 在防火墙后面使用谷歌地图 api