简体   繁体   English

添加了 Google 地图 api v3 标记,但地图未缩放

[英]Google maps api v3 Marker is added but the map is not zooming

Somedays ago , i was having this problem of not knowing how to get data from php with ajx and json, which i solved on myself, here is the link of my complete code几天前,我遇到了这个问题,不知道如何使用 ajx 和 json 从 php 获取数据,这是我自己解决的,这是我完整代码的链接

How to get Coordinates from database on server, on checkbox checked and show on googlemaps? 如何从服务器上的数据库获取坐标,选中复选框并显示在谷歌地图上?

this is the client side code which gets coordinates through ajax request and add a marker on google maps when checkbox is checked, and removes it when un_checked.这是客户端代码,它通过ajax请求获取坐标,并在选中复选框时在谷歌地图上添加一个标记,并在取消选中时将其删除。

code代码

function get_coordinates(checkbox){
    var v_id=checkbox.id;
    if(checkbox.checked){
        var hr = new XMLHttpRequest();
        hr.open("POST", "fetch_coordinates.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var data = JSON.parse(hr.responseText);
                var lat=data.loc.lat;
                var lon=data.loc.lon;
                addmarker(lat,lon,v_id);
            }
        }
         hr.send("id="+v_id);   
    } else{
        var mark = markers[v_id]; // find the marker by given id
        mark.setMap(null);
        delete markers[v_id];
    }    
}
function addmarker(lat,lon,v_id){
    var marker = new google.maps.Marker({
        id: v_id,
        position: new google.maps.LatLng(lat, lon),
        zoom: 8,    //not working
        map: map,
        title: 'Vehicle No: '+v_id
    });
    markers[v_id] = marker;

}

Now when i add a marker, i set my zoom level to 8, but the map doesnot zoom, i dont know why but it stands still, just adds a marker , but is not zooming.现在,当我添加一个标记时,我将缩放级别设置为 8,但是地图没有缩放,我不知道为什么,但是它静止不动,只是添加了一个标记,但没有缩放。 whay is that?那是什么?

A marker doesn't have a zoom -property, adding this property will have no effect.标记没有zoom属性,添加此属性将不起作用。

You must set the properties for the map:您必须设置地图的属性:

map.setOptions({center:new google.maps.LatLng(lat, lon),zoom:8});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM