0

一段时间以来,我一直在尝试为每个标记创建一个信息窗口,但无法使其正常工作。

这就是我想出的:

for(var i=0; i<markery.length; i++)
{
    var latt =  parseFloat(markery[i].attributes.getNamedItem("lat").nodeValue);
    var lon =   parseFloat(markery[i].attributes.getNamedItem("lon").nodeValue);
    var ikona_url = markery[i].attributes.getNamedItem("ikona").nodeValue;
    var nazwa = markery[i].attributes.getNamedItem("nazwa").nodeValue;
    var rozmiar = new google.maps.Size(30,23);
    var punkt_startowy = new google.maps.Point(0,0);
    var punkt_zaczepienia = new google.maps.Point(15,12);
    var ikona = new google.maps.MarkerImage(ikona_url, rozmiar, punkt_startowy, punkt_zaczepienia);
    markert.push(new google.maps.Marker({
                    position: new google.maps.LatLng(latt,lon),
                    title: nazwa,
                    icon: ikona,
                    map: map,
                    content: nazwa
                }));

    google.maps.event.addListener(marker, 'click', function() {
        var info = new google.maps.InfoWindow({content: this.content});
    });
}

完整的代码是:

<script type="text/javascript">

var map;
var marker1;
var markert = [];
var lati;
var loni;
var infowindow;

我开始地图:

    function initialize() {

  lat = 50.42952;
  long = 15.60059;
  var latlng = new google.maps.LatLng(lat, long);
   var myOptions = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        draggableCursor:'crosshair',
                };

     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
dymek = new google.maps.InfoWindow();

    google.maps.event.addListener(map, 'click', function(event) {
        add_marker(event.latLng, 'Your new marker', 'Your new marker' );
    });

}

该函数获取点击点的地址:

     function findAddress(event) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({latLng: event.latLng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {

          infoWindow.setContent(results[0].formatted_address);
          infoWindow.setPosition(event.latLng);
          infoWindow.open(map);
        }
      }
    });
  }

此功能在我单击的地图上添加了一个新标记:

function add_marker( pos, pos_title, pos_str ) {

    marker1 = new google.maps.Marker( {
        position: pos,
        map: map,
        draggable: true,
        title: pos_title
    });

    map.setZoom(15);
    map.setCenter(marker1.getPosition());
    LoadMarkers();
                    }

此函数加载我在上面的函数中创建的标记附近的附近点:

function LoadMarkers()
        {

var adres='add.xml?lat='+lati+'&long='+loni;
            jx.load(adres, function(xml)
            {
                var markery = xml.getElementsByTagName("marker");
                for(var i=0; i<markery.length; i++)
                {
                    var latt            =   parseFloat(markery[i].attributes.getNamedItem("lat").nodeValue);
                    var lon         =   parseFloat(markery[i].attributes.getNamedItem("lon").nodeValue);
                    var ikona_url   =   markery[i].attributes.getNamedItem("ikona").nodeValue;
                    var nazwa       =   markery[i].attributes.getNamedItem("nazwa").nodeValue;
                var markert     =   addMarkers(latt,lon,ikona_url,nazwa);



                }

            },'xml','get');
        }

这个函数实际上是在地图上广告附近的标记。这些标记是我希望信息窗口显示的标记:

    function addMarkers(latt,lon,ikona_url,nazwa)
        {
            var rozmiar = new google.maps.Size(30,23);   
            var punkt_startowy = new google.maps.Point(0,0);   
            var punkt_zaczepienia = new google.maps.Point(15,12);     
            var ikona = new google.maps.MarkerImage(ikona_url, rozmiar, punkt_startowy, punkt_zaczepienia); 
            markert.push(new google.maps.Marker({
                         position: new google.maps.LatLng(latt,lon),
                         title: nazwa,
                         icon: ikona,
                         map: map,
                         animation: google.maps.Animation.DROP }));

 google.maps.event.addListener(markert, 'tilesloaded', function() {
    var info = new google.maps.InfoWindow({content: nazwa});
});

        }

4

1 回答 1

0

您实际上并没有在任何时候打开信息窗口。请查看此文档和示例,并相应地调整您的代码。您实际上需要open使用infowindow.

于 2012-06-13T03:41:33.920 回答