Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图如何添加标记,如果我知道纬度和经度_Javascript_Jquery_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 谷歌地图如何添加标记,如果我知道纬度和经度

Javascript 谷歌地图如何添加标记,如果我知道纬度和经度,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,之前,我在单击时添加标记,如下所示: google.maps.event.addListener(map, 'click', function(event) { marker = new google.maps.Marker({ position: event.latLng, map: map }); 这就是find,但现在我想添加一个标记,它取决于纬度和经度,而不是单击事件 我试过这个: if (("#latitude").val() &

之前,我在单击时添加标记,如下所示:

google.maps.event.addListener(map, 'click', function(event) {
    marker = new google.maps.Marker({
        position: event.latLng,
        map: map
    });
这就是find,但现在我想添加一个标记,它取决于纬度和经度,而不是单击事件

我试过这个:

if (("#latitude").val() && (("#longitude").val())){
    marker = new google.maps.marker({
    });
}
但我仍然不知道应该传递什么参数。你能帮我吗

非常感谢


<!DOCTYPE html>
<html>
  <head>
    <title>Add Marker to Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 90%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script>
        var map;
        function initialize() {
          var mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(-34.397, 150.644)
          };
          map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        }
        google.maps.event.addDomListener(window, 'load', initialize);

        jQuery(document).ready(function(){

            jQuery("#addmarker").bind("click", function(){
                console.log("Click");
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng( -34.397,150.644),
                    map: map,
                    title: 'Hello World!'
                });

                var contentString = '<div id="content" style="width: 200px; height: 200px;"><h1>Overlay</h1></div>';
                var infowindow = new google.maps.InfoWindow({
                    content: contentString
                });

                google.maps.event.addListener(marker, 'click', function() {
                  infowindow.open(map,marker);
                });

                // To add the marker to the map, call setMap();
                marker.setMap(map);
            });
        });     
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
    <a href="javascript:void(0);" id="addmarker">Add marker</a>
  </body>
</html>
将标记添加到地图 html,正文,#地图画布{ 身高:90%; 边际:0px; 填充:0px } var映射; 函数初始化(){ 变量映射选项={ 缩放:8, 中心:新google.maps.LatLng(-34.397150.644) }; map=new google.maps.map(document.getElementById('map-canvas'),mapOptions); } google.maps.event.addDomListener(窗口“加载”,初始化); jQuery(文档).ready(函数(){ jQuery(#addmarker”).bind(“单击”,函数(){ 控制台日志(“单击”); var marker=new google.maps.marker({ 位置:新google.maps.LatLng(-34.397150.644), 地图:地图, 标题:“你好,世界!” }); var contentString='Overlay'; var infowindow=new google.maps.infowindow({ 内容:contentString }); google.maps.event.addListener(标记'click',函数(){ 信息窗口。打开(地图、标记); }); //要将标记添加到映射,请调用setMap(); marker.setMap(map); }); });

触发标记的事件是什么?我得到了
未捕获的TypeError:undefined不是这一行的函数
如果(((“#纬度”).val()和((“#经度”).val()){
问题不在答案中,这是测试代码的重要一行答案已编辑,请重试,但您的答案没有提供
if
语句。错误消息在
if
条件中,我必须解决它以测试您的代码。我希望您得到我的答复您应该使用LatLng而不是#latude和#longtudentity我得到了
未捕获类型错误:未定义不是这一行的函数
if(((“#纬度”).val())和((“#经度”).val()){
你能再试一次我的代码吗?
这是我的努力,但我不能尝试整个代码,我已经有一个复杂的页面,我只是想添加标记,但现在我告诉你,我在
if
语句中收到一条错误消息。所以我需要解决它,以便能够访问
if
条件。然后测试你的代码。你能吗请检查错误?这是对这个问题的第一个注释。许多感谢您提供了两个ID为
latitude
longitude
的元素。请尝试以下操作:
if(document.getElementById(“latitude”).value&&document.getElementById(“longitude”).value)marker=new google.maps.marker({position:new google.maps.LatLng(document.getElementById(“纬度”).value,document.getElementById(“经度”).value),map:map,title:'Hello World!';}
map.addOverlay(new GMarker(
    new GLatLng(-33.9417, 150.9473)));
var point = new GLatLng(-33.9417, 150.9473);
map.setCenter(point, 10);
var marker = new GMarker(point);
map.addOverlay(marker);