10

I'm trying to create a button that will add a marker to an existing google maps that is displayed.

function initialize()
{
    geocoder = new google.maps.Geocoder();
    codeAddress();
}



function codeAddress()
{
    var image_icon = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';
    var address = document.getElementById("type_location").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            var mapOptions = {
            zoom: 11,
            center: results[0].geometry.location,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: image_icon
            });

        }           

    });

}

I'm fairly new and I was hoping someone can help me.

If I have something like this to display my maps:

$(document).ready(function() {  

var coord = $(".address").attr("data-coordinates"); //this displays lat,lng (example: 32.000,-118.000)

var geocoder;
var map;    
initialize();
$(".add_marker").click(function(){

    // this is where I should add a marker?
});

});
2
  • Yes. give an alert inside that to check whether you are clicking the correct button Commented Nov 7, 2012 at 7:33
  • Well does it work? Do you get any error? I would recommend to create the map outside of the codeAddress function, because you don't need to create a new map everytime the user clicks, right? Commented Nov 7, 2012 at 9:27

2 Answers 2

21

lucky you!

i have a working example that does exactly what you want :)

see the full code and test it here: http://jsfiddle.net/RASG/vA4eQ/
just click the button "add marker"

here is the relevant code:

var latlng = new google.maps.LatLng(42.745334, 12.738430);

function addmarker(latilongi) {
    var marker = new google.maps.Marker({
        position: latilongi,
        title: 'new marker',
        draggable: true,
        map: map
    });
    map.setCenter(marker.getPosition())
}

$('#btnaddmarker').on('click', function() {
    addmarker(latlng)
})
Sign up to request clarification or add additional context in comments.

Comments

2

HTML for map

<div id="map" style="width:100%;height:500px" ></div>

javascript for loading map and marker

function myMap() {
    var myCenter = new google.maps.LatLng(28.214461283939166,83.95801313236007);
    var map_Canvas = document.getElementById("map");
    var mapOptions = {center: myCenter, zoom: 14,mapTypeId:google.maps.MapTypeId.HYBRID};
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);
    google.maps.event.addListener(map, 'click', function(event) {
        document.getElementById("latitude").value = map.getCenter().lat(); 
        document.getElementById("longitude").value = map.getCenter().lng(); 
      place_Marker(map, event.latLng);

    });

  }
  var marker;
  function place_Marker(map, location) {
      if(marker){
          marker.setPosition(location);
      }else{
      marker = new google.maps.Marker({
      draggable:true,
      position: location,
      animation: google.maps.Animation.DROP,
      map: map
    });
  }

    var infowindow = new google.maps.InfoWindow({
      content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng()
    });

    infowindow.open(map,marker);

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.