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

3

之前,我是通过点击添加标记的方式来实现的:

google.maps.event.addListener(map, 'click', function(event) {
    marker = new google.maps.Marker({
        position: event.latLng,
        map: map
    });

之前这个功能可以正常工作,但现在我希望根据经纬度添加标记,而不是仅在点击事件中添加。

我尝试了以下代码:

if (("#latitude").val() && (("#longitude").val())){
    marker = new google.maps.marker({
    });
}

但我仍然不知道应该传递哪个参数。你能帮我吗?

非常感谢。


什么事件会触发标记? - John
2个回答

5
<!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>

我在这行代码上遇到了 Uncaught TypeError: undefined is not a function 的错误:if((("#latitude").val()) && (("#longitude").val())){ - Anastasie Laurent
你能再试一次我的代码吗? - Ashwin Parmar
感谢您的努力,但我无法尝试整个代码,因为我已经有一个复杂的页面,我只想添加标记,但现在正如我告诉您的那样,我在“if”语句中收到了错误消息。所以我需要解决它才能访问“if”条件。然后测试您的代码。您能检查一下这个错误吗?这是对这个问题的第一个评论。非常感谢。 - Anastasie Laurent
你们有ID为latitudelongitude的两个元素吗? - Ashwin Parmar
不要担心,使用控制台进行调试,它会给出值。 - Ashwin Parmar
显示剩余8条评论

0
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);

http://javascript.about.com/library/blgmap05.htm


我在这行代码 if((("#latitude").val()) && (("#longitude").val())){ 上遇到了 Uncaught TypeError: undefined is not a function 的错误。 - Anastasie Laurent
重要的是测试你的代码行是否正确,这在问题而非答案中。 - Anastasie Laurent
答案已被编辑,请重新尝试。 - Rachel Gallen
抱歉,您的答案没有提供if语句。错误消息在if条件中,我必须解决它才能测试您的代码。希望您明白我的意思。 - Anastasie Laurent
您应该使用 LatLng 而不是 #latitude 和 #longtitude。 - Rachel Gallen
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接