How to show all the markers in google map on initial load? - google-maps

I have developed a project which shows google map with multiple markers. My problem is that not all markers are shown on the initial load. ie. if i have four markers, only 2 are visible in the map when the map initially load.I have to zoom out to view the rest of the markers.
How can i solve the issue ?
Heres my code for getting Googlemap via javascript
window.onload=function(){
var mapOptions={
center:new google.maps.LatLng(markers[0].lat,markers[0].lng),
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var infoWindow=new google.maps.InfoWindow();
var map=new google.maps.Map(document.getElementById("dvMap"),mapOptions);
var bounds=new google.maps.LatLngBounds();
for(i=0;i<markers.length;i++){
var data=markers[i];
var latlng=new google.maps.LatLng(data.lat,data.lng);
var marker=new google.maps.Marker({
position:latlng,
map:map,
title:data.title
});
(function(marker,data){
google.maps.event.addListener(marker,"click",function(e){
infoWindow.setContent(data.description);
infoWindow.open(map,marker);
});
})(marker,data);
}
}

It's been a while since I did Google Maps API work but I believe what you need is this:
var bounds = new google.maps.LatLngBounds ();
// Go through each...
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
// And increase the bounds to take this point
bounds.extend (LatLngList[i]);
}
// Fit these bounds to the map
map.fitBounds (bounds);
You already define the bounds array and loop your markers to just add each marker to the bounds array and then fitBounds.
See http://blog.shamess.info/2009/09/29/zoom-to-fit-all-markers-on-google-maps-api-v3/ for the original article.

for(i=0;i<markers.length;i++){
var data=markers[i];
var latlng=new google.maps.LatLng(data.lat,data.lng);
var marker=new google.maps.Marker({
position:latlng,
map:map,
title:data.title
});
bounds.extend(marker); // here is the code to add every marker plotted on bounds
(function(marker,data){
google.maps.event.addListener(marker,"click",function(e){
infoWindow.setContent(data.description);
infoWindow.open(map,marker);
});
})(marker,data);
}
map.setCenter(latlngbounds.getCenter()); // this will set the center of map to center of all markers
map.fitBounds(latlngbounds); // this will fit all the markers to screen

Related

Google map in responsive map doesn't center map

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...

marker icon on get directions google map

I have a question about placing markers on the google maps get directions. I have this script which on page load renders map however, I want to put company logo on the this map. I mean like initial map without any directions.
src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script><script type="text/javascript">// <![CDATA[
var map;
var directionsDisplay;
var manila = new google.maps.LatLng(55.009657,-1.450706);
var directionsService = new google.maps.DirectionsService();
function initialize() {
// Make the route draggable
var rendererOptions = {
draggable: true,
}
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var mapOptions = {
zoom:13,
mapTypeId: google.maps.MapTypeId.DRIVING,
center: manila,
}
map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}
I know that in order to have icon on a map you need to create marker object...
var marker=new google.maps.Marker({
position:manila,
icon:'image-link'
});
and to use this marker you set map:
marker.setMap(map);
But I am using directionsDisplay.setMap(map); instead of marker.setMap(map); how I can pass this marker object to directionsService map?
G.I.Y.F This page explains how to add your own logo to a Google Map.
See also: How to add logo to copyrights in google maps?
http://www.developerdrive.com/2013/08/adding-a-custom-icon-to-a-google-maps-marker/
etc

Remove a marker in google maps API V3

in an app building environment I'm trying to use this code:
var map = new Appery('googlemap').gmap;
var markerLatLng = new google.maps.LatLng(localStorage.getItem("markerLat"), localStorage.getItem("markerLng"));
var Titolo = 'Some Title';
var markers = [];
var marker = new google.maps.Marker({
position: markerLatLng,
map: map,
title: Titolo,
animation: google.maps.Animation.DROP
});
markers.push(marker);
function setAllMap(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
google.maps.event.addListener(marker, 'click', function() {markers.pop();
map.refresh();
setAllMap(map);
});
but it doesn't works. Could anyone say me why? I just want to remove the last marker that the user made by clicking on any marker she/he wants.
Thank you
Removing the marker object from the array will not remove the marker from the map. You have to call setMap(null) on the marker to do so. Just do markers[markers.length -1].setMap(null) and then pop() on the array;

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 map api V2 Marker Issue

I have the following code
var marker;
var marker_list = [];
for (iLoopIndex=0;iLoopIndex<10;iLoopIndex++)
{
centerPoint = new GLatLng(32+iLoopIndex,68+iLoopIndex);
alert(centerPoint);
map.setCenter(centerPoint);
blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.image = "truck.png";
blueIcon.iconSize = new GSize(40, 20);
// Set up our GMarkerOptions object
markerOptions = { icon:blueIcon };
//map.addOverlay(new GMarker(centerPoint, markerOptions));
marker = new GMarker(centerPoint, markerOptions);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml("iLocator <b>"+Myarr[2]+"</b>");
marker_list.push(marker);
});
map.addOverlay(marker);
}//End for
This Code make 10 markers on Google map, now I want to remove the markers, following is the code to remove the markers.
for (iLoopIndex=0;iLoopIndex<marker_list.length;iLoopIndex++)
{
map.removeOverlay(marker_list[iLoopIndex]);
}
This code is not working its only remove the infowindow from the marker but not removing the image. Kindly guide me what I am doing wrong.
You're pushing your markers into your marker_list array inside the callback function for the GEvent Listener that you registered.. Your array will only be populated with Markers that had their InfoWindow triggered.
Move "marker_list.push(marker);" to the line above "map.addoverlay(marker);" ie..
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml("iLocator <b>"+Myarr[2]+"</b>");
});
marker_list.push(marker);
map.addOverlay(marker);
}//End for