如何使用多个地图指针制作谷歌地图,以在打开页面时缩放特定区域/位置

How to make Google Map with Multiple Map Pointers to zoom a particular area/location on opening a page

本文关键字:缩放 位置 区域 地图 何使用 指针 谷歌地图      更新时间:2023-09-26

在有多个地图指针的谷歌地图中,我无法缩放到特定区域/位置。目前,它会自动将地图居中,覆盖所有位置。但我需要的是在打开网页时放大到一个特定的位置。这是我的代码,

<script>
jQuery(function ($) {
    // Asynchronously Load the map API 
    var script = document.createElement('script');
    script.src = "//maps.googleapis.com/maps/api/js?key=AIzaSyA157quXbUlHHgm4wJJxzD49MivKgPSil8&sensor=false&callback=initialize";
    document.body.appendChild(script);
});
function initialize() {
    locations = <?php echo json_encode($sl_select_obj) ?>;
    markerdata = JSON.parse('<?php echo json_encode($locations_data) ?>');

    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: 'roadmap',
    };
    // Display a map on the page
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    map.setTilt(45);
    // Multiple Markers''
    // $info='';
    var markers = locations;
    <?php
    $total = count($locations_data);
    $markerinfo = '';
    $c = 0;
    foreach ($locations_data as $key => $info):
        if ($c != $total):
            $markerinfo .="['<div class='"info_content'"><h2>" . $info['loctitle'] . "</h2><h3><a href=" . $info['site'] . ">" . $info['site'] . "</a></h3><h3><a href=" . $info['locpermalink'] . ">View Full Office Details</a></h3></div>'],";
        else:
            $markerinfo .="['<div class='"info_content'"><h2>" . $info['loctitle'] . "</h2><h3><a href=" . $info['site'] . ">" . $info['site'] . "</a></h3><h3><a href=" . $info['locpermalink'] . ">View Full Office Details</a></h3></div>']";
        endif;
        $c++;
    endforeach;
    ?>

    // Info Window Content                        
    var infoWindowContent = [<?php echo $markerinfo; ?>]; //markerdata; 

    // Display multiple markers on a map
    var infoWindow = new google.maps.InfoWindow(), marker, i;
    // Loop through our array of markers & place each one on the map  
    for (i = 0; i < markers.length; i++) {
        var position = new google.maps.LatLng(markers[i]["lat"], markers[i]["long"]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i]["address"],
            icon: '<?php bloginfo('template_directory'); ?>/images/venue-direct-icon.png'
        });
        // Allow each marker to have an info window    
        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));
        // Automatically center the map fitting all markers on the screen
        map.fitBounds(bounds);
    }
    // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {
        this.setZoom(7);
        google.maps.event.removeListener(boundsListener);
    });
    map.fitBounds(markerBounds);
}

尝试添加这样的"center"选项:

var mapOptions = {
    center: new google.maps.LatLng(51.5287718, -0.2416803),
    mapTypeId: 'roadmap',
    zoom: 15,
    draggable: map_draggable,
    scrollwheel: map_scrollwheel
};

编辑:为了帮助您,这是一个类似于我在项目中使用的代码:多个位置,如果只有一个,则以第一个为中心,否则我对所有位置使用谷歌自动中心。

jQuery( document ).ready( function( $ ) {
if($(window).width()>991){
    var map_draggable = true;
    var map_scrollwheel = false;
} else {
    var map_draggable = false;
    var map_scrollwheel = false;
}
var offices = [];
$('#map-google-container')
    .find('.hidden')
    .each(function(){
        offices[offices.length] = 
        {
            "latitude":$(this).attr('latitude'),
            "longitude":$(this).attr('longitude'),
            "content":$(this).attr('content')
        };
    });
var mapOptions = {
    zoom: 6,
    center: new google.maps.LatLng(offices[0].latitude, offices[0].longitude),
    draggable: map_draggable,
    scrollwheel: map_scrollwheel,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
    panControl: true
};
var map = new google.maps.Map(document.getElementById('map-google'), mapOptions);
var map_styles = [
  {
    featureType: "all",
    stylers: [
        { hue: "#F69200" }, 
        { saturation: -50 }
    ]
  },{ 
    featureType: "water", 
    stylers: [ 
        { color: "#efefef" } 
    ] 
  } 
];
map.setOptions({styles: map_styles});
var image = '/include/images/marker.png';
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < offices.length; i++)
{  
    var content = offices[i].content;
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(offices[i].latitude, offices[i].longitude),
        map: map,
        icon: image,
        animation: google.maps.Animation.DROP,
        title: content
    });
    bounds.extend(marker.position);
    var infowindow = new google.maps.InfoWindow();
    google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
        return function() {
           infowindow.setContent(content);
           infowindow.open(map,marker);
        };
    })(marker,content,infowindow));
}
if (offices.length > 1) { map.fitBounds(bounds); }
});

在我的项目中,我必须列出所有的办公室,并展示一张包含所有办公室的独特地图。因此,我在HTML中放入了所有办公室的信息(为用户隐藏)。在JavaScript中,我循环浏览这些属性并填充映射。

希望它能帮助

是的,我可以找到解决方案,因为那些面临类似问题的人可能会觉得它很有帮助,

var map_center_position = new google.maps.LatLng(34.0059657 ,-118.4440441);
        bounds.extend(map_center_position);
map.fitBounds(bounds);