Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  87] [ 4]  / answers: 1 / hits: 30459  / 13 Years ago, fri, october 21, 2011, 12:00:00

I am adding markers to Google Maps dynamically using jquery and Google Maps V3 API. When the button search_button is clicked, a request is sent to the server using AJAX, which returns a JSON array of LatLng corresponding to the results, which will be used to place the markers. However in my Javascript Conole, I get the error: Invalid value for property <map>: map. Where did I go wrong? Here's my code:



HTML Header JS:



<script type=text/javascript>
function initialize() {
var latlng = new google.maps.LatLng(42.354183,-71.065063);
var options = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById(map_canvas), options);
}
</script>


jQuery



$(function() {

$(#search_button).click(function(e){
e.preventDefault();


// Place markers on map
for( i = 0; i < json.length; i++) {
var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}

});
});
});

More From » jquery

 Answers
9

you should make global your variable called map. That's all, in fact my recommendation it's to move all to a javascript file like this



    $(document).ready(initialize);
var map;

function initialize() {
var latlng = new google.maps.LatLng(42.354183,-71.065063);
var options = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map($('#map-canvas')[0], options);

$(#search_button).click(function(e){
e.preventDefault();


// Place markers on map
for( i = 0; i < json.length; i++) {
var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}

});
}

[#89506] Wednesday, October 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irvingcarloe

Total Points: 677
Total Questions: 109
Total Answers: 96

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
irvingcarloe questions
Wed, Mar 31, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Fri, Jul 3, 20, 00:00, 4 Years ago
;