-1

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

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.

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. whay is that?

Community
  • 1
  • 1
user3493077
  • 101
  • 1
  • 4

1 Answers1

0

A marker doesn't have a zoom-property, adding this property will have no effect.

You must set the properties for the map:

map.setOptions({center:new google.maps.LatLng(lat, lon),zoom:8});
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • ok, but my next step will be adding multiple markers, you are setting center to one marker, i dont want to do it, i want to center it on all the markers, how to do that? – user3493077 Apr 04 '14 at 21:13
  • by the way, thanks for the anwer, it has worked for now, just tell me how to focus all the markers in a map? – user3493077 Apr 04 '14 at 21:15
  • @user3493077 What do you mean by focusing all markers? Do you want to set center of the map to the center of all markers? If so, check: http://stackoverflow.com/questions/3520810/zoom-and-center-a-google-map-according-to-its-markers-javascript-api-v3 – alpakyol Apr 05 '14 at 07:57