How to show info window? - google-maps

Hi guys been trying my best to solve this problem. I can't see info window even after clicking the marker
<script type="text/javascript">
function initialize() { //Initalize JS after onload
var map = new google.maps.Map(document.getElementById('map_canvas'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
});
var randomPoint0 = new google.maps.LatLng(18.9750, 72.8258); //First Location
var marker0 = new google.maps.Marker({ //Set up marker
position: randomPoint0,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid0'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker0.getPosition());
});
var randomPoint1 = new google.maps.LatLng(19.8833, 76.7833); //First Location
var marker1 = new google.maps.Marker({ //Set up marker
position: randomPoint1,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid1'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker1.getPosition());
});
var randomPoint2 = new google.maps.LatLng(25.6000, 85.1167); //First Location
var marker2 = new google.maps.Marker({ //Set up marker
position: randomPoint2,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid2'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker2.getPosition());
});
var randomPoint3 = new google.maps.LatLng(18.5333, 73.8667); //First Location
var marker3 = new google.maps.Marker({ //Set up marker
position: randomPoint3,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid3'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker3.getPosition());
});
var randomPoint4 = new google.maps.LatLng(22.3000, 70.7833); //First Location
var marker4 = new google.maps.Marker({ //Set up marker
position: randomPoint4,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid4'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker4.getPosition());
});
var randomPoint5 = new google.maps.LatLng(17.8625, 78.886); //First Location
var marker5 = new google.maps.Marker({ //Set up marker
position: randomPoint5,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid5'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker5.getPosition());
});
var randomPoint6 = new google.maps.LatLng(13.0833, 80.2833); //First Location
var marker6 = new google.maps.Marker({ //Set up marker
position: randomPoint6,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid6'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker6.getPosition());
});
var randomPoint7 = new google.maps.LatLng(28.6167, 77.2167); //First Location
var marker7 = new google.maps.Marker({ //Set up marker
position: randomPoint7,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid7'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker7.getPosition());
});
var randomPoint8 = new google.maps.LatLng(19.9833, 73.8000); //First Location
var marker8 = new google.maps.Marker({ //Set up marker
position: randomPoint8,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid8'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker8.getPosition());
});
var randomPoint9 = new google.maps.LatLng(30.7343, 76.7933); //First Location
var marker9 = new google.maps.Marker({ //Set up marker
position: randomPoint9,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid9'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker9.getPosition());
});
var randomPoint10 = new google.maps.LatLng(22.5697, 88.3697); //First Location
var marker10 = new google.maps.Marker({ //Set up marker
position: randomPoint10,
map: map
});
google.maps.event.addDomListener(document.getElementById('locationid10'), 'click', //Set up DOM listener 1
function(){
map.setZoom(13);
map.setCenter(marker10.getPosition());
});
map.setCenter(marker0.getPosition());
map.setZoom(5);
var info = message;
var infoWindow = new google.maps.InfoWindow({
content: message
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, marker);
});
}
</script>
this is my link
http://www.safarikidindia.com/safari-map.php#
All the marker pins are dynamic so how I can show address text, can you please advice

You need to had an infoWindow for each marker!
Supposing you have 10 different messages, stored in 10 variables (message0, message1, message2,...), you should write:
var infoWindow0 = new google.maps.InfoWindow({
content: message0
});
google.maps.event.addListener(marker0, 'click', function () {
infoWindow0.open(map, marker0);
});
var infoWindow1 = new google.maps.InfoWindow({
content: message1
});
google.maps.event.addListener(marker1, 'click', function () {
infoWindow1.open(map, marker1);
});
var infoWindow2 = new google.maps.InfoWindow({
content: message2
});
google.maps.event.addListener(marker2, 'click', function () {
infoWindow2.open(map, marker2);
});
...(and so on until marker10)

Related

Google Maps API - Activating the infowindow of a custom marker

I am trying to activate the infowindow for a custom marer, yielding a small description about the marker, I'm having some trouble at the moment, I have the custom marker working but I can't get the infowindow to show.
I tried calling the listener for the marker and storing it in a variable "customMarker", then calling another mouseover listener to activate the infowindow, but I'm having no luck, can anyone help me out?
var map;
//Creates a custom icon to be placed on the map
var goldStar = 'https://cdn2.iconfinder.com/data/icons/august/PNG/Star%20Gold.png';
function initialize() {
//Sets the zoom amount for the map, the higher the number, the closer the zoom amount
var mapOptions = {
zoom: 18
//center : myLatLng
};
//The map object itself
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var contentString = 'This is a custom toolTip';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
// Tries to find user location using HTML 5
if(navigator.geolocation)
{
//sets the map to the position of the user using location
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
var customMarker = google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng, map);
});
//This listener is not working
//google.maps.event.addListener(customMarker, 'mouseover', function() {
//infowindow.open(map,customMarker);
//});
}
function placeMarker(location, map)
{
var marker = new google.maps.Marker({
position: location,
icon: goldStar,
map: map,
title: "custom marker",
draggable:true
});
map.panTo(location);
}
The google.maps.event.addListener function does not return a marker. This won't work:
var customMarker = google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng, map);
});
Assign the event listener in your placeMarker function to the marker you create (also gives the advantage of maintaining function closure on the marker):
function placeMarker(location, map) {
var marker = new google.maps.Marker({
position: location,
icon: goldStar,
map: map,
title: "custom marker",
draggable: true
});
var contentString = 'This is a custom toolTip';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.open(map,marker);
});
map.panTo(location);
}
working fiddle

Check DistanceWidget's marker dragend

DistanceWidget:
function DistanceWidget(map,markers) {
this.set('map', map);
this.set('position', map.getCenter());
this.set('markers',markers);
// MARKER CENTER
var marker = new google.maps.Marker({
draggable: true,
icon: {
url: "/public/img/move.png",
size: new google.maps.Size(20, 20),
anchor: new google.maps.Point(10, 10)
},
title: 'Di chuyển'
});
marker.bindTo('map', this);
marker.bindTo('position', this);
var me = this;
var radiusWidget = new RadiusWidget(markers);
this.bindTo('circle', radiusWidget);
radiusWidget.bindTo('map', this);
radiusWidget.bindTo('markers', this);
radiusWidget.bindTo('center', this, 'position');
this.bindTo('distance', radiusWidget);
this.bindTo('bounds', radiusWidget);
this.bindTo('radius', radiusWidget);
}
RadiusWidget:
RadiusWidget.prototype.center_changed = function() {
var bounds = this.get('bounds');
if (bounds) {
var lng = bounds.getNorthEast().lng();
var position = new google.maps.LatLng(this.get('center').lat(), lng);
this.set('sizer_position', position);
}
};
RadiusWidget.prototype.checkCenterDragend = function(){
//TODO SOMETHING HERE
}
I move (drag) MARKER CENTER in DistanceWidget, center_changed prototype will update. That's great.
Question: How can I check event dragend of MARKER CENTER to update something in checkCenterDragend prototype.
Thank for your time.
Simply call the method in the dragend-handler:
place this after the line var radiusWidget = new RadiusWidget(markers);
google.maps.event.addListener(marker, 'dragend', function() {
radiusWidget.checkCenterDragend.call(radiusWidget,this.getPosition());
});
The first argument passed to the function will be the marker-position(the center)

Show all infowindows open

I'm trying to have custom infowindows float above markers, however I noticed that only one marker can be opened at any one time. Is there a workaround to this?
Here's the code I have produced at the moment:
downloadUrl("AllActivityxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("id");
var address = markers[i].getAttribute("id");
var type = markers[i].getAttribute("venue_type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng"))
);
var infowindow = new google.maps.InfoWindow();
var html = "<b>" + point + "</b>hello <br/>" + type;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
markersArray.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map,marker);
});
}
});
And when you check this map, notice that I cannot open more than 2 infowindows at once. Why is that?
There is no limitation implicit to the Google Maps API v3 that makes only one InfowWindow available at a time. You need to write your code to do that. If you want an InfoWindow for each marker, make one.
Some thing like (not tested):
function createMarker(latlng, html) {
var contentString = html;
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
position: latlng,
map: map,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
}

Google Map Zoom To Fit

I have developed a map that uses a geo-coordinate from a database using a cluster marker example. I have tried to make it zoom automaticly by using LatLngBounds();
<script type="text/javascript">
function initialize() {
var center = new google.maps.LatLng(9.4419, 9.1419);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
// create the infowindow out of the for boucle
var infoWindow = new google.maps.InfoWindow;
var bounds = new google.maps.LatLngBounds();
for ( var i = 0; i < latlng.length; i++ ) {
var latLng = new google.maps.LatLng(latlng.lat,latlng.lng);
var html = 'test show in infowondow';
var marker = new google.maps.Marker({
position: latLng,
map: map,
icon:"/img/icon1.jpg",
title:"test title",
});
// call to the function....
bindInfoWindow(marker, map, infoWindow, html);
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
}
// create a function bindInfoWindow
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
make bounds global.
then after
var latLng = new google.maps.LatLng(latlng.lat,latlng.lng);
EDIT: and as Marcelo pointed out change this to:
var latLng = new google.maps.LatLng(latlng[i].lat,latlng[i].lng);
add:
bounds.extend(latLng)
after
var markerCluster = new MarkerClusterer(map, markers);
add:
map.fitBounds(bounds);

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);
});