Google zoom level for country

I am having a problem with how to set the zoom level for different countries, I managed to get the map to work and display the country, I just can't figure out how to set the zoom level.

Any help would be appreciated.

thank

George

<script type="text/javascript">
                    var infowindow = null;
                    $(document).ready(function () { initialize(); });

                    function initialize() {

                        //var geocoder = new google.maps.Geocoder();
                        //geocoder.geocode({ 'address': address }, function (results, status) {
                        //    if (status == google.maps.GeocoderStatus.OK) {
                        //        map.setCenter(results[0].geometry.location);
                        //        map.fitBounds(results[0].geometry.viewport);
                        //    }
                        //});

                        var centerMap = new google.maps.LatLng(@Html.Raw(@item.strLatLong));

                        var myOptions = {
                            zoom: 4, //<<-------How can I chnage this
                            center: centerMap,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                        }

                        var map = new google.maps.Map(document.getElementById("WeatherMapLocation"), myOptions);

                        setMarkers(map, sites);
                        infowindow = new google.maps.InfoWindow({
                            content: "loading..."
                        });

                        var bikeLayer = new google.maps.BicyclingLayer();
                        bikeLayer.setMap(map);
                    }



                    function setMarkers(map, markers) {

                        for (var i = 0; i < markers.length; i++) {
                            var sites = markers[i];
                            var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
                            var marker = new google.maps.Marker({
                                position: siteLatLng,
                                map: map,
                                title: sites[0],
                                zIndex: sites[3],
                                html: sites[4]
                            });

                            var contentString = "Some content";

                            google.maps.event.addListener(marker, "click", function () {
                                infowindow.setContent(this.html);
                                infowindow.open(map, this);
                            });
                        }
                    }
                </script>

      

+3


source to share


2 answers


From the geocoder documentation, there is a viewport and bounds returned in the geocoder's response that can be used to center and scale to the result.

 if (results && results[0] && results[0].geometry && results[0].geometry.viewport) 
      map.fitBounds(results[0].geometry.viewport);

      



working example

+3


source


Fixed issue after more research found this link.

https://developers.google.com/maps/documentation/javascript/geocoding

      



George

0


source







All Articles