I am new to google maps and following is code snippet of google maps
The marker is in middle of the screen but how can I appear at top center (top middle as illustrated in image) of the screen.
function initMap() {
const myLatLng = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: myLatLng,
});
new google.maps.Marker({
position: myLatLng,
map,
title: "Hello World!",
});
}
window.initMap = initMap;
2
Answers
Simply, to the specified location you can use the
Map.panTo()
method and to set the zoom level you can use theMap.setZoom()
method.You can test it to see if it’s useful to you, and if you want any changes, just let me know and I’ll make them.
PanTo()
: https://developers.google.com/maps/documentation/javascript/reference/map#Map.panToThis function is used to set the center position of the map:
panTo(latLng)
Parameters:
latLng: LatLng|LatLngLiteral The new center latitude/longitude of the map.
Return Value: None
Changes the center of the map to the given LatLng. If the change is less than both the width and height of the map, the transition will be smoothly animated.
So this will you to set center position according to marker.
This will also help you:
Google Maps PanTo OnClick
UPDATE
http://jsfiddle.net/kamrankb/6mxopg0n/2/