try it. hope this helps. 1. Make a map of the global variable. 2. initialize the map 3. add a marker to the button click event.
<script type="text/javascript"> var map; function initialize() { map = new google.maps.Map(document.getElementById('mapCanvas'), { zoom: 8, center: latLng, mapTypeId: google.maps.MapTypeId.ROADMAP }); }; jQuery("$addmarker").click(function(){ var marker = new google.maps.Marker({ position: new google.maps.LatLng(23.72, 72.100), title: 'Marker', map: map, draggable: true }); }) </script>
Here is my complete code example.
<script type="text/javascript"> var map; function initialize() { var mapOptions = { center: new google.maps.LatLng('23.11', '71.00'), zoom: 2, scrollwheel: false, disableDefaultUI: false, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions); } function addMarker() { marker = new google.maps.Marker({ position: new google.maps.LatLng(23.72, 72.100), map: map, }); } </script> </HEAD> <BODY onload="initialize();"> <div id="map_canvas" style="width:700px; height:500px;"></div> <input type="button" id="addMarker" value="addMarker" onclick="addMarker();"/> </BODY> </HTML>
source share