SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Google Maps API Zoom Not Working For 1 Location

    Hi All
    My script for google maps works fine for multiple locations but if their is only 1 location
    its fully zoomed in. Any ideas full code below

    <? // get business locations //
    $query = "SELECT locationAddress FROM tblClientLocations WHERE clientID='$part1'";
    $result = mysql_query($query,$db);
    while($row = mysql_fetch_array($result))
    {
    $locationAddress = $row[locationAddress];
    $locationAddress = stripslashes($locationAddress);

    $locationAddress = str_replace("<br>", "+", $locationAddress);
    $locationAddress = str_replace(" ", "+", $locationAddress);

    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$locationAddress&sensor=false&region=$region");
    $json = json_decode($json);

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

    ?>
    ['TEST', <? echo $lat;?>, <? echo $long;?>],
    <?
    }
    ?>
    ];

    function initializeMaps() {
    var myOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
    var infowindow = new google.maps.InfoWindow();
    var marker, i;
    var bounds = new google.maps.LatLngBounds();

    for (i = 0; i < markers.length; i++) {
    var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
    bounds.extend(pos);
    marker = new google.maps.Marker({
    position: pos,
    map: map

    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
    infowindow.setContent(markers[i][0]);
    infowindow.open(map, marker);
    }
    })(marker, i));
    }
    map.fitBounds(bounds);
    }
    </script>

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    41
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It happens because you're calling "fitBounds" - the API will zoom as far as it can go to display all the markers. If you only have 1 marker, your map will be completely zoomed in

    Solution can be found here: http://stackoverflow.com/questions/2...sing-fitbounds


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •