开发者

Problems using LatLngBounds and the center of a map

开发者 https://www.devze.com 2023-03-06 03:22 出处:网络
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 s

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\",\"C开发者_运维百科lientID\":\"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?


$.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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消