Google map in responsive map doesn't center map - google-maps

I have a problem with google map. I embeeded it into responsive web. Everything's fine, map behaves responsivly, but when I change the window size, for example open the page on a mobile phone, it just cuts from the map, when it's resizing, but I need it to center the map. I need the locations be still visible and I don't know how to do it. Could anyone help me? Hope my question is comprehensible. Thanks.
API call:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
JS code:
function initialize() {
var myLatlng = new google.maps.LatLng(48.652645, 21.083107);
var mapOptions = {
zoom: 11,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
setMarkers(map, beaches);
}
var beaches = [
['xxxx'],
['xxxx']
];
function setMarkers(map, locations) {
for (var i = 0; i < locations.length; i++) {
var beach = locations[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);

Create a bounds global variable.
Extend the bounds object when adding your markers.
On window resize, trigger a map resize and make the map fit the bounds object.
Global variable:
var bounds = new google.maps.LatLngBounds();
Extend the bounds in the setMarkers function:
for (var i = 0; i < locations.length; i++) {
var beach = locations[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map
});
bounds.extend(myLatLng);
}
Add this to your initialize function:
window.addEventListener("resize", resizeUI);
Create a resize function:
function resizeUI() {
google.maps.event.trigger(map, 'resize');
map.fitBounds(bounds);
}
Untested, but you get the idea...

Related

Google maps clear all markers before placing new one

I'm trying to place a marker on click in google maps along with populating input boxes with the lat and lng. I need the code to clear all the existing markers first before placing the new marker and updating the lat and lng. everything works except when I add code to try clear all the markers.
The below code works perfectly but does not clear the old markers before placing the new one.
Thank you.
Working code without clearing existing markers...
<div id="map"></div>
<script>
var map;
function initMap() {
var latlng = new google.maps.LatLng(-29, 25);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: latlng,
});
google.maps.event.addListener(map, 'click', function(event){
var marker_position = event.latLng;
marker = new google.maps.Marker({
map: map,
draggable: false
});
marker.setPosition(marker_position);
document.getElementById("latFld").value = event.latLng.lat();
document.getElementById("lngFld").value = event.latLng.lng();
})
}
</script>
Not working code with clearing markers before adding a new one...
<div id="map"></div>
<script>
var map;
var markersArray = [];
function initMap() {
var latlng = new google.maps.LatLng(-29, 25);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: latlng,
});
markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(event){});
google.maps.event.addListener(map, 'click', function(event){
var marker_position = event.latLng;
marker = new google.maps.Marker({
map: map,
draggable: false
});
clearOverlays();
marker.setPosition(marker_position);
document.getElementById("latFld").value = event.latLng.lat();
document.getElementById("lngFld").value = event.latLng.lng();
})
}
function clearOverlays() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
}
Thank you.
seem you have placed the code in the wrong place .. try
<div id="map"></div>
<script>
var map;
var markersArray = [];
function initMap() {
var latlng = new google.maps.LatLng(-29, 25);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: latlng,
});
clearOverlays();
marker = new google.maps.Marker({
map: map,
draggable: false
});
google.maps.event.addListener(marker,"click",function(event){});
google.maps.event.addListener(map, 'click', function(event){
var marker_position = event.latLng;
marker.setPosition(marker_position);
markersArray.push(marker);
document.getElementById("latFld").value = event.latLng.lat();
document.getElementById("lngFld").value = event.latLng.lng();
})
}
function clearOverlays() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
markersArray = [];
}
</script>

Google Maps Pin on Wrong Lat,Long and Zooming In and Out changes pin location

I am facing an issue on Google maps where the initial location of the pin is dropped at the wrong location even with the right Lat and Long provided. Also, when I zoom in and out of the map, the pin will disappear and reappear at another location which is wrong as well.
Images for better explanation: Images
ko.bindingHandlers.plotCoordinates = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var mapCanvas = $(element).parent().find('.map-canvas')[0];
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
for (var index in bindingContext.$data.AnalyticsData().LocationInfo()) {
var eachLocation = bindingContext.$data.AnalyticsData().LocationInfo()[index];
var image = "/Images/Location-Icon-Blue copy.png";
var position = new google.maps.LatLng(eachLocation.Latitude(), eachLocation.Longitude());
console.log("lat: "+eachLocation.Latitude());
console.log("long: "+eachLocation.Longitude());
bounds.extend(position);
var marker = new google.maps.Marker({
position: position,
map: map,
icon: image
});
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(15);
google.maps.event.removeListener(boundsListener);
});
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
}
}
I resolved it. There was a css:
canvas{
width:400px;
height:600px;
}
that was making the marker skewed.

change the place of the marker with the click

i am working on google maps, what i want to do is to click on the map it add a marker to the map its working fine but the problem is that i need to move the same marker to a new place if i click some where else instead of new marker. and also when i click on any point it give me the address of that place in a text field
here is my code
<script type="text/javascript">
var map;
var markersArray = [];
function initialize() {
var haightAshbury = new google.maps.LatLng(37.7699298, -122.4469157);
var mapOptions = {
zoom: 7,
center: haightAshbury,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
google.maps.event.addListenerOnce(map, 'click', function(event) {
addMarker(event.latLng);
});
}
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map
});
markersArray.push(marker);
}
// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
}
}
}
You can call clearOverlays anytime you create a new marker.this way you hide the previous marker and only the new is shown.
another way is to have a var marker just as you have a var map. you can edit the markers position in addMarker.
as far as geocoding is concerned take a look at this

Google maps API multiple markers

I am trying to push some markers into google maps using the following code. But it does not seem working. The map is getting centred to the right position but the I can see the markers.
var points = [<asp:literal runat="server" id="litPoints"/>];
$(document).ready(function () {
var mapCenter = new google.maps.LatLng(<asp:literal runat="server" id="litMapCentre"/>);
var options = {
zoom:<asp:literal runat="server" id="litZoomLevel"/>,
center: mapCenter,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($("#monitorMap")[0], options);
GetMap(map, points);
});
function GetMap(map, mappoints) {
var image=new google.maps.MarkerImage('../Images/map/iconr.png',
new google.maps.Size(20,32),
new google.maps.Point(0,0));
for(var i=1; i < points.length; i++) {
var m=points[i];
var mylatlng=new google.maps.LatLng(m[0], m[1]);
var marker=new google.maps.Marker({
position: mylatlng,
map: map,
icon: image});
}
}
Change the second function to be like this:
function GetMap(map, mappoints) {
var image=new google.maps.MarkerImage('../Images/map/iconr.png',
new google.maps.Size(20,32),
new google.maps.Point(0,0));
for(var i=0; i < mappoints.length; i++) {
var m=mappoints[i];
var mylatlng=new google.maps.LatLng(m[0], m[1]);
var marker=new google.maps.Marker({
position: mylatlng,
map: map,
icon: image});
}
You were creating a variable called mappoints, but then referring to it as points. Also javascript arrays are zero-indexed, so if looping over them you usually need to start at 0, not 1.

InfoWindow on Marker using MarkerClusterer

This is my html code. I've try anything to add an infowindow on the markers but it don't wanna work. My data is loading from the "Alle_Ortswahlen.page1.xml" file.
Do anyone have an idea how can I add infoWindow to each marker?
<script type="text/javascript">
google.load('maps', '3', {
other_params: 'sensor=false'
});
google.setOnLoadCallback(initialize);
function initialize() {
var stack = [];
var center = new google.maps.LatLng(48.136, 11.586);
var options = {
'zoom': 5,
'center': center,
'mapTypeId': google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), options);
GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
var xmlDoc = GXml.parse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("Field4"));
var lng = parseFloat(markers[i].getAttribute("Field6"));
var marker = new google.maps.Marker({
position : new google.maps.LatLng(lat, lng),
map: map,
title:"This is a marker"
});
stack.push(marker);
}
var mc = new MarkerClusterer(map,stack);
});
}
</script>
Before the for cycle, make an empty infowindow object.
var infowindow = new google.maps.InfoWindow();
Than in the for cycle, after the marker, add an event listener, like this:
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent("You might put some content here from your XML");
infowindow.open(map, marker);
}
})(marker, i));
There is some closure magic happening when passing the callback argument to the addListener method. If you are not familiar with it, take a look at here:
Mozilla Dev Center: Working with Closures
So, your code should look something like this:
google.load('maps', '3', {
other_params: 'sensor=false'
});
google.setOnLoadCallback(initialize);
function initialize() {
var stack = [];
var center = new google.maps.LatLng(48.136, 11.586);
var options = {
'zoom': 5,
'center': center,
'mapTypeId': google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), options);
var infowindow = new google.maps.InfoWindow();
GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
var xmlDoc = GXml.parse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("Field4"));
var lng = parseFloat(markers[i].getAttribute("Field6"));
var marker = new google.maps.Marker({
position : new google.maps.LatLng(lat, lng),
map: map,
title:"This is a marker"
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent("You might put some content here from your XML");
infowindow.open(map, marker);
}
})(marker, i));
stack.push(marker);
}
var mc = new MarkerClusterer(map,stack);
});
}
So what you need to do is add some code, inside your for-loop, associating an infowindow onclick event handler with each marker. I'm assuming you only want to have 1 infowindow showing at a time, i.e. you click on a marker, the infowindow appears with relevant content. If you then click on another marker, the first infowindow disappears, and a new one reappears attached to the other marker. Rather than having multiple infowindows all visible at the same time.
GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
var xmlDoc = GXml.parse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
// just create one infowindow without any content in it
var infowindow = new google.maps.InfoWindow({
content: ''
});
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("Field4"));
var lng = parseFloat(markers[i].getAttribute("Field6"));
var marker = new google.maps.Marker({
position : new google.maps.LatLng(lat, lng),
map: map,
title:"This is a marker"
});
// add an event listener for this marker
google.maps.event.addListener(marker , 'click', function() {
// assuming you have some content in a field called Field123
infowindow.setContent(markers[i].getAttribute("Field123"));
infowindow.open(map, this);
});
stack.push(marker);
}
var mc = new MarkerClusterer(map,stack);
});