如何添加标记到谷歌地图 - JavaScript

时间:2014-11-26 20:08:41

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

我是java脚本的初学者。我想在地图上添加一个标记。现在我该怎么办?

我想设置这些职位:LatLng(35.738943, 51.326078);

我的地图:

    <script type="text/javascript">
        // When the window has finished loading create our google map below
        google.maps.event.addDomListener(window, 'load', init);

        function init() {
            // Basic options for a simple Google Map
            // For more options see: https://developers.google.com/maps/documentation/javascript/reference#MapOptions
            var mapOptions = {
                // How zoomed in you want the map to start at (always required)
                zoom: 17,

                // The latitude and longitude to center the map (always required)
                center: new google.maps.LatLng(35.738943, 51.326078),

                // How you would like to style the map. 
                // This is where you would paste any style found on Snazzy Maps.
                styles: [   {       featureType:"all",      elementType:"all",      stylers:[       {           invert_lightness:true       },      {           saturation:10       },      {           lightness:30        },      {           gamma:0.5       },      {           hue:"#1C705B"       }       ]   }   ]
            };

            // Get the HTML DOM element that will contain your map 
            // We are using a div with id="map" seen below in the <body>
            var mapElement = document.getElementById('map');

            // Create the Google Map using out element and options defined above
            var map = new google.maps.Map(mapElement, mapOptions);
        }
    </script>
     <script src="js/wow.min.js"></script>
 <script>
 wow = new WOW(
 {

    }   ) 
    .init();
</script> 

1 个答案:

答案 0 :(得分:0)

var lat = "35.738943";
var lng = "51.326078";
var name = "My place";

var myLatlng = new google.maps.LatLng(lat,lng);

创建地图后立即:

var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: name
    });

这就是你的整个代码可能看起来像

google.maps.event.addDomListener(window, 'load', init);

        function init() {

            var lat = "35.738943";
            var lng= "51.326078";
            var name = "My place";

            var myLatlng = new google.maps.LatLng(lat,lng);

            var mapOptions = {                
                zoom: 17,
                center: new google.maps.LatLng(35.738943, 51.326078),                
                styles: [   {       featureType:"all",      elementType:"all",      stylers:[       {           invert_lightness:true       },      {           saturation:10       },      {           lightness:30        },      {           gamma:0.5       },      {           hue:"#1C705B"       }       ]   }   ]
            };

            var mapElement = document.getElementById('map');

            var map = new google.maps.Map(mapElement, mapOptions);

            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: name
            });