admin管理员组文章数量:1203823
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
});
}
});
});
});
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
});
}
});
});
});
Share
Improve this question
asked Oct 21, 2011 at 3:24
NyxynyxNyxynyx
63.6k163 gold badges505 silver badges855 bronze badges
1
- How do you perform the ajax call first and then load the maps with markers? Because map is loaded on window onload and ajax call is performed later so lat/lng are received later after the map has been loaded. Can you please help me I am stuck on this for hours. How can I force the Ajax call to go first and get the array and then load the map with marker? – Faizan Commented Oct 29, 2013 at 18:17
1 Answer
Reset to default 20you 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
});
}
});
}
本文标签: javascriptDynamically Add Google Map V3 Markers using jQueryStack Overflow
版权声明:本文标题:javascript - Dynamically Add Google Map V3 Markers using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738673622a2106136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论