3

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

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Anastasie Laurent
  • 1,169
  • 6
  • 23
  • 35

2 Answers2

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>
Ashwin Parmar
  • 3,025
  • 3
  • 26
  • 42
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

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81