【问题标题】:Problems using LatLngBounds and the center of a map使用 LatLngBounds 和地图中心的问题
【发布时间】:2011-08-24 19:11:57
【问题描述】:

显示地图时遇到问题。 我有一组标记(通过 php 和 json 来自 db),我希望相对于所有标记应用地图的中心和缩放;这就是为什么不在地图上设置中心和缩放并使用 LatLngBounds 对象的原因。问题是,它不起作用。

这是javascript代码:

var jsonURL="http://localhost/gpsdev/db2json.php";
var map; 


function init(){    

    var options = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl:false,
        scaleControl:true,
        streetViewControl:false,
        zoom:12,
        center: new google.maps.LatLng(-25.973728,32.582745)                                 
    };                                                   

    var mapBounds= new google.maps.LatLngBounds();       
    map = new google.maps.Map(document.getElementById('map'), options);                                                                       

    $.getJSON(jsonURL,{cli:"1"}, function(data){
        for (var i = 0; i < data.length; i++) {
            var point = new google.maps.LatLng(data[i].Latitude,data[i].Longitude);
            mapBounds.extend(point);            
            new google.maps.Marker({
                position: point, 
                map: map, 
                title:data[i].PlateNR
            });
        }
    }); 
    map.fitBounds(mapBounds);
}

window.onload=init;

如果我删除缩放并居中它不起作用。这是在 javascript 上输入的 json:

[
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.973728\",\"Longitude\":\"32.582745\",\"Velocity\":\"0.000\",\"Ignition\":\"0\",\"ClientName\":\"Sergio\"},
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.972456\",\"Longitude\":\"32.578968\",\"Velocity\":\"10.000\",\"Ignition\":\"1\",\"ClientName\":\"Sergio\"},
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.970083\",\"Longitude\":\"32.580879\",\"Velocity\":\"80.000\",\"Ignition\":\"1\",\"ClientName\":\"Sergio\"},
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.968191\",\"Longitude\":\"32.577724\",\"Velocity\":\"90.000\",\"Ignition\":\"1\",\"ClientName\":\"Sergio\"}
]

这里做错了什么?

【问题讨论】:

    标签: php json google-maps google-maps-api-3


    【解决方案1】:
    $.getJSON(jsonURL,{cli:"1"}, function(data){
        for (var i = 0; i < data.length; i++) {
            var point = new google.maps.LatLng(data[i].Latitude,data[i].Longitude);
            mapBounds.extend(point);            
            new google.maps.Marker({
                position: point, 
                map: map, 
                title:data[i].PlateNR
            });
        }
        // this is where you should call map.fitBounds()
        map.fitBounds(mapBounds);
    }); 
    // move the following line from its existing location to inside $.getJSON call
    // map.fitBounds(mapBounds);
    

    【讨论】:

      【解决方案2】:

      查看本教程:http://www.svennerberg.com/2008/11/bounding-box-in-google-maps/,但请注意它与最新的 API (v3) 无关。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多