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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24511138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:56:50  来源:igfitidea点击:

google maps how to add marker if I know the latitude and longitude

javascriptjquerygoogle-mapsgoogle-maps-api-3

提问by Anastasie Laurent

Before, I was adding the marker on click like this:

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

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

and that was working find, but now I want to add a marker depending on the latitude and longitude not depending on the click event.

那是有效的查找,但现在我想根据纬度和经度添加一个标记,而不是根据点击事件。

I tried this:

我试过这个:

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

but I still don't know what parameter should I pass. Could you help me please?

但我仍然不知道我应该传递什么参数。请问你能帮帮我吗?

many thanks

非常感谢

回答by Ashwin Parmar

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

回答by Rachel Gallen

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

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