2017-08-28 110 views
-1

下面的代码用于显示我的网站上谷歌地图上的位置。它在桌面视图上正常工作,但对于响应不起作用。善良帮助我调整缩放和大小,并使其响应。我附上了图片以供参考。响应谷歌地图

地图上有四个标记。 3个是簇,一个是正常标记。请帮助我调整视图,以便我的所有标记在一帧中可见。如果您点击标记时可以帮助我平稳缩放,这也会非常有帮助。

谢谢。

SCREENSHOT FOR DESKTOP VIEW

desk view

function initMap() { 

    // Create a new StyledMapType object, passing it an array of styles, 
    // and the name to be displayed on the map type control. 
    var styledMapType = new google.maps.StyledMapType(

[ 
    { 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#f5f5f5" 
     } 
    ] 
    }, 
    { 
    "elementType": "labels.icon", 
    "stylers": [ 
     { 
     "visibility": "off" 
     } 
    ] 
    }, 
    { 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#616161" 
     } 
    ] 
    }, 
    { 
    "elementType": "labels.text.stroke", 
    "stylers": [ 
     { 
     "color": "#f5f5f5" 
     } 
    ] 
    }, 
    { 
    "featureType": "administrative.land_parcel", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#bdbdbd" 
     } 
    ] 
    }, 
    { 
    "featureType": "poi", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#eeeeee" 
     } 
    ] 
    }, 
    { 
    "featureType": "poi", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#757575" 
     } 
    ] 
    }, 
    { 
    "featureType": "poi.park", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#e5e5e5" 
     } 
    ] 
    }, 
    { 
    "featureType": "poi.park", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#9e9e9e" 
     } 
    ] 
    }, 
    { 
    "featureType": "road", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#ffffff" 
     } 
    ] 
    }, 
    { 
    "featureType": "road.arterial", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#757575" 
     } 
    ] 
    }, 
    { 
    "featureType": "road.highway", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#dadada" 
     } 
    ] 
    }, 
    { 
    "featureType": "road.highway", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#616161" 
     } 
    ] 
    }, 
    { 
    "featureType": "road.local", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#9e9e9e" 
     } 
    ] 
    }, 
    { 
    "featureType": "transit.line", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#e5e5e5" 
     } 
    ] 
    }, 
    { 
    "featureType": "transit.station", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#eeeeee" 
     } 
    ] 
    }, 
    { 
    "featureType": "water", 
    "elementType": "geometry", 
    "stylers": [ 
     { 
     "color": "#c9c9c9" 
     } 
    ] 
    }, 
    { 
    "featureType": "water", 
    "elementType": "labels.text.fill", 
    "stylers": [ 
     { 
     "color": "#9e9e9e" 
     } 
    ] 
    } 
], 
     {name: 'Styled Map'}); 
function initialize() { 

var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

// Resize stuff... 
google.maps.event.addDomListener(window, "resize", function() { 
    var center = map.getCenter(); 
    google.maps.event.trigger(map, "resize"); 
    map.setCenter(center); 
}); 

    // Create a map object, and include the MapTypeId to add 
    // to the map type control. 

    var myLatlng = {lat: 29.687978, lng: 0.00}; 

    var map = new google.maps.Map(document.getElementById('map'), { 
    center: myLatlng, 
    zoom: 3, 
    mapTypeControlOptions: { 
     mapTypeIds: ['roadmap', 'satellite', 'hybrid', 'terrain', 
       'styled_map'] 
    } 
    }); 

    var marker = locations.map(function(location, i) { 
     return new google.maps.Marker({ 
     position: location, 
     animation: google.maps.Animation.DROP, 
     title: 'Click to zoom' 

    }); 

    marker.addListener('click', toggleBounce); 
    }); 

    // Add a marker clusterer to manage the markers. 
    var markerCluster = new MarkerClusterer(map, marker, 
     {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'}); 

    //Associate the styled map with the MapTypeId and set it to display. 
    map.mapTypes.set('styled_map', styledMapType); 
    map.setMapTypeId('styled_map'); 
    map.setOptions({ minZoom: 2, maxZoom: 15 }); 

} 


var locations = [ 
    {lat: 19.013916, lng: 73.012211}, 
    {lat: 18.995356, lng: 72.838849}, 
    {lat: 42.365981, lng: -71.051223}, 
    {lat: 18.558610, lng: 73.883862}, 
] 

function toggleBounce() { 
    if (marker.getAnimation() !== null) { 
    marker.setAnimation(null); 
    } else { 
    marker.setAnimation(google.maps.Animation.BOUNCE); 
    } 
} 
/* Always set the map height explicitly to define the size of the div 
* element that contains the map. */ 
#map { 
    height: 100%; 
} 
/* Optional: Makes the sample page fill the window. */ 
html, body { 
    height: 400px; 
    width: 100%; 
    margin: 0; 
    padding: 0; 
} 
div id="map"></div> 


<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script> 
+0

看起来好像你正在尝试添加一些你正在做/正在做的教程。特别是[其中之一](https://developers.google.com/maps/documentation/javascript/)。您应该先尝试不同的事情,例如[尝试搜索](https://www.google.com/search?q=google+maps+api+responsive&oq=google+maps+api+responsive&aqs=chrome .. 69i57j0l5.6960j0j7&sourceid = chrome&ie = UTF-8),因为已经有大量数据可以执行此操作。然后,阅读[如何问](https://stackoverflow.com/help/how-to-ask),然后在这里提问。 SO不是研究的替代方案... – Nukeface

+0

[如何使Google地图响应?](https://stackoverflow.com/questions/34317386/how-to-make-google-maps-responsive) – Nukeface

回答

0

此链接会帮助你。 stackoverflow/googlemap.resize

不使用图像即时的地图。地图调整了函数大小,在window.resize事件中调用它。