0

你好,

这是谷歌的正常代码

var map;

function initialize()
{
    var latLng = new google.maps.LatLng(41.079120183660486, 28.994637246032653);

    var mapOptions = {
        center: latLng,
        zoom: 8,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    var marker = new google.maps.Marker({
        position: map.getCenter(),
        map: map,
    });

    google.maps.event.addListener(map, 'click', function (event) {
        //map.setZoom(9);
        //map.setCenter(marker.getPosition());

    });


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

我想从我的数据库中乘以标记,但我做不到。我在网站上搜索但总是结果是灰色地图。

我的代码是:

<script type="text/javascript">
var map;
var latLng;
function initialize() {

    var mapOptions = {
        //center: latLng,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    var markers = [];

    @foreach (var item in Model)
{
        @:markers.push("@item.map")
        // item.map content coming like this : 41.079120183660486,28.994637246032653
}
    for (var i = 0; i < markers.length; i++) {

        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
        });
    }

    //google.maps.event.addListener(map, 'click', function (event) {
    //    //map.setZoom(9);
    //    map.setCenter(marker.getPosition());
    //    //placeMarker(event.latLng);
    //});

}

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

我无法添加标记。我觉得错过了很多东西?我真的需要你的意见。谢谢

4

1 回答 1

0

根据您的代码,您似乎正在向相同的 lat/lng 添加标记。下面的代码在循环中添加了一些标记。

<script>
    var map;
    var center = new google.maps.LatLng(39.715, -84.103);

    $(document).ready(initialize);

    function initialize() {
        // Create map
        var mapOptions = {
            zoom: 10,
            center: center,
            panControl: false,
            zoomControl: true,
            mapTypeControl: true,
            scaleControl: true,
            streetViewControl: true,
            overviewMapControl: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map($('#map_canvas')[0], mapOptions);

        for (var ii = 0; ii < 5; ii++) {
            var lat = center.lat() + (0.1 * ii);
            var lng = center.lng() + (0.1 * ii);
            var loc = new google.maps.LatLng(lat, lng);
            var marker = new google.maps.Marker({
                position: loc,
                map: map
            });
        }
    }
</script>
于 2013-01-24T16:46:45.950 回答