使用 LatLngBounds 和地图中心的问题

发布于 2024-11-07 00:17:47 字数 2197 浏览 1 评论 0原文

显示地图时遇到问题。 我有一个标记集合(通过 php 和 json 来自数据库),我希望相对于所有标记应用地图的中心和缩放;这就是为什么我不在地图上设置中心和缩放并使用 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\"}
]

这里做错了什么?

Am having a problem showing a map.
I have a collection of markers (from a db via php and json) and i want the center and the zoom for the map applied relative to all of the markers; Thats why am not setting the center and the zoom on the map and using the LatLngBounds object. Thing is, its not working.

This is the javascript code:

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;

if i remove the zoom and center it doesnt work. this is the json am inputting on the javascript:

[
{\"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\"}
]

What am doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-11-14 00:17:47
$.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);
$.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);
白况 2024-11-14 00:17:47

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

Check this tutorial: http://www.svennerberg.com/2008/11/bounding-box-in-google-maps/, but be careful its not about the latest API (v3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文