0

首先,我使用来自http://www.pittss.lv/jquery/gomap/examples/marker.php的库,并且我使用网络服务来获取数据。

我想在地图上找到这些信息,但我以前不知道我必须创建多少元素。要获取数据,我有以下代码:

    $('#default-search').submit(function () {

    var gomap_marker = [];
    var search_where = $('#search-where').val();
    if ($('#search-where').val() == "")
        search_where = "-1";
    $.ajax({
        url: "http://my_url.com/",
        contentType: "json",
        data: {
            city: search_where
        },
        success: function (data) {

            //List received data
            $.each(data, function (index, item) {

                //create an array with data
                gomap_marker.push({
                    id: item.Id,
                    address: item.Street + ', ' + item.ZipCode + ' ' + item.City, 
                    icon: 'images/marker.png',
                    group: 'toshow',
                    html: '<a href="index.html">' + item.Street + ', ' + item.ZipCode + ' ' + item.City + '</a>'
                });
            }); // END - $.ajax success each

            // valid test
            for (var i = 0; i < gomap_marker.length; i++) {
                alert('1 - ' + gomap_marker[i].id + gomap_marker[i].address + gomap_marker[i].html);
            }

            // reset the map for each request
            $("#map").removeData();
            alert('Test');
            // map init + markers
            jQuery('#map').goMap({
                maptype: 'ROADMAP',
                zoom: 13,
                scaleControl: true,
                scrollwheel: false,
                markers: gomap_marker
            });
        }, // END - $.ajax success 
        error: function () { alert("simple request goes wrong"); }
    }); // END - $.ajax
    return false;
}); // END - default-search

只有我数组的第一行在地图上创建了一个标记。我不明白如何设置其他人。

我想使用 $.goMap.createMarker 而不是创建数组并重置地图,但我遇到了同样的问题:只有第一行在地图上创建了一个标记。

问候,

4

1 回答 1

0

emm 首先创建一个 lat,long 数组

    var data = new Array();
    data[0] = new Array();
    data[0][0] = 'FOOD'; //name
    data[0][1] = '23,23';//coordinates(lat,lng)

    function HTMLMarker( place ) {
           this.name = place[0];
           var latLngStrings = place[1].split(',');
           var lat = +latLngStrings[0];
           var lng = +latLngStrings[1];
           this.pos = new google.maps.LatLng( lat, lng );
    }
    HTMLMarker.prototype = new google.maps.OverlayView();
    HTMLMarker.prototype.onRemove = function () {};
    HTMLMarker.prototype.onAdd = function () {
    var div = this.div = document.createElement('DIV');
    div.className = "htmlMarker";
    div.innerHTML = '<a href="#" class="pin_on_map">'+this.name+'</a>';
                        var panes = this.getPanes();
                        panes.overlayImage.appendChild(div);
                    };
   HTMLMarker.prototype.draw = function () {
          var overlayProjection = this.getProjection();
          var position = overlayProjection.fromLatLngToDivPixel(this.pos);
          var panes = this.getPanes();
          this.div.style.left = position.x - 30 + 'px';
          this.div.style.top = position.y - 48 + 'px';
                    };

     function initialize() {
          var myLatLng = new google.maps.LatLng(23,23);
          var mapOptions = {
                            zoom: 15,
                            center: myLatLng,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                        };

     var gmap = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

                        for (var i = 0; i < data.length; i++) {
                            addMarker( data[i] );
                        }

                        function addMarker( place ) {
                            var htmlMarker = new HTMLMarker( place );
                            htmlMarker.setMap(gmap);
                        }
                    }

                   google.maps.event.addDomListener( window, 'load', initialize );

这应该工作!我正在用 php 创建数组:

<script>
<?php $i = 0;
foreach($places as $place) {
     $pins = $place['pins']; //coordinates
     $name = $place['name'];
     echo "data[$i] = new Array();\n";
     echo "data[$i][0] = '" .$name. "';\n";
     echo "data[$i][1] = '" .$pins. "';\n";
  $i++;
} ?>
</script>

也在css中添加这个

 .htmlMarker {
    position: absolute;
}
于 2013-09-02T11:51:34.720 回答