Javascript 谷歌地图-在地图上动态生成标记

Javascript 谷歌地图-在地图上动态生成标记,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我“我是谷歌地图的新手,我试图让搜索页面上返回的所有位置都出现在相应的地图上,但我无法让它正常工作。我让每个列表显示它们的经度和纬度,并使用下面的代码。有人能帮我解决这个问题吗?一定是我错过了一些简单的东西。谢谢 jQuery(function($) { // Asynchronously Load the map API var script = document.createElement('script'); script.src = "http://maps.g

我“我是谷歌地图的新手,我试图让搜索页面上返回的所有位置都出现在相应的地图上,但我无法让它正常工作。我让每个列表显示它们的经度和纬度,并使用下面的代码。有人能帮我解决这个问题吗?一定是我错过了一些简单的东西。谢谢

jQuery(function($) {
    // Asynchronously Load the map API 
    var script = document.createElement('script');
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
    document.body.appendChild(script);
});

function initialize() {
    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: 'roadmap'
    };

    // Display a map on the page
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    map.setTilt(45);

    // Multiple Markers

    var markers = [];
     $(".eventCoordinates").each(function() {
        var myLocation = $(this).html();
        markers.push(myLocation);

    });

    // Info Window Content
    var infoWindowContent = [
        ['<div class="info_content">' +
        '<h3>London Eye</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>' +        '</div>'],
        ['<div class="info_content">' +
        '<h3>Palace of Westminster</h3>' +
        '<p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p>' +
        '</div>']
    ];

    // Display multiple markers on a map
    var infoWindow = new google.maps.InfoWindow(), marker, i;

    // Loop through our array of markers & place each one on the map  
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });

        // Allow each marker to have an info window    
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));

        // Automatically center the map fitting all markers on the screen
        map.fitBounds(bounds);
    }

    // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(14);
        google.maps.event.removeListener(boundsListener);
    });

}

 </script>           

            <div id="map_wrapper">
    <div id="map_canvas" class="mapping"></div>
</div>

<div class="event">

<div class="eventCoordinates">51.503454,-0.119562</div>

</div>

<div class="event">

<div class="eventCoordinates">51.499633,-0.124755</div>

</div>
jQuery(函数($){
//异步加载映射API
var script=document.createElement('script');
script.src=”http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(脚本);
});
函数初始化(){
var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
mapTypeId:“路线图”
};
//在页面上显示地图
map=new google.maps.map(document.getElementById(“map_canvas”),mapOptions);
地图.设置倾斜(45);
//多重标记
var标记=[];
$(“.eventCoordinates”).each(函数(){
var myLocation=$(this.html();
标记。推(myLocation);
});
//信息窗口内容
var infoWindowContent=[
['' +
“伦敦眼”+
“伦敦眼是一个巨大的摩天轮,位于泰晤士河岸边。整个结构高135米(443英尺),车轮直径120米(394英尺)。

”+“, ['' + “威斯敏斯特宫”+ “威斯敏斯特宫是英国议会两院下议院和上议院的会址。通常以其租户而称为议会大厦。

”+ ''] ]; //在地图上显示多个标记 var infoWindow=new google.maps.infoWindow(),marker,i; //在我们的标记阵列中循环并将每个标记放置在地图上 对于(i=0;i
要从html中获取数据,需要将html转换为数字数组

尝试:


以下是一个工作示例:

jQuery(函数($){
//异步加载映射API
var script=document.createElement('script');
script.src=”http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(脚本);
});
函数初始化(){
var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
mapTypeId:“路线图”
};
//在页面上显示地图
map=new google.maps.map(document.getElementById(“map_canvas”),mapOptions);
地图.设置倾斜(45);
//多重标记
var标记=[];
$(“.eventCoordinates”)。每个(函数(){
var myLocation=$(this.html().split(',');
markers.push(['Marker',parseFloat(myLocation[0]),parseFloat(myLocation[1]));
});
//信息窗口内容
var infoWindowContent=[
['' +
“伦敦眼”+
“伦敦眼是一个巨大的摩天轮,位于泰晤士河岸边。整个结构高135米(443英尺),车轮直径120米(394英尺)。

”+“, ['' + “威斯敏斯特宫”+ “威斯敏斯特宫是英国议会两院下议院和上议院的会址。通常以其租户而称为议会大厦。

”+ ''] ]; //在地图上显示多个标记 var infoWindow=new google.maps.infoWindow(),marker,i; //在我们的标记阵列中循环并将每个标记放置在地图上 对于(i=0;i
.mapping{
高度:480px;
宽度:640px;
}

51.503454,-0.119562
51.499633,-0.124755

需要查看转换为标记的
$(“.eventCoordinates”)
中html的外观。请记住,这只是一个stri
$(".eventCoordinates").each(function() {
        // create array of strings
        var myLocation = $(this).html().split(',');
        // convert strings to number
        myLocation[0] = + myLocation[0];
        myLocation[1] = + myLocation[1];
        markers.push(myLocation);
});