Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Google Maps API: Drop marker at CURRENT viewed center?

    Im just playing around, i currently have a button that when clicks, will drop a marker to the center of the map (its the center of the ORIGINAL viewed area), how can i modify it to drop a marker at the CURRENTLY viewed center?

    Many thanks

    Code:
    			var centerpoint = new google.maps.LatLng(-34.397, 150.644);
         function initialize() {
                var mapOptions = {
                    center: centerpoint,
                    zoom: 10,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
    				
    			var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    			
    			
    			
    			document.getElementById('createMarker').onclick = function () {
    			var marker = new google.maps.Marker({
    			position: centerpoint,
    			map: map,
    			});
    			}
    
    			}
    
    			google.maps.event.addDomListener(window, 'load', initialize);

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I guess it would be best to use the getCenter method:

    Code:
    document.getElementById('createMarker').onclick = function () {
    			var marker = new google.maps.Marker({
    			position: map.getCenter(),
    			map: map,
    			});

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply xela. I actually did employ that but it seems to be taking me to the center of the original view point (i.e the same was when the map first loads?)

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't see that with this code (which is basically what you posted...)
    Code:
    var centerpoint = new google.maps.LatLng(-34.397, 150.644);
    
    			function initialize() {
    			  var mapOptions = {
    			    center: centerpoint,
    			    zoom: 10,
    			    mapTypeId: google.maps.MapTypeId.ROADMAP
    			  };
    
    			  var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    
    
    			  document.getElementById('createMarker').onclick = function () {
    			    var marker = new google.maps.Marker({
    			      position: map.getCenter(),
    			      map: map,
    			    });
    
    			  }
    
    
    			}
    
    
    			google.maps.event.addDomListener(window, 'load', initialize);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •