Google Maps show marker inside a cluster - google-maps

Is it possible to zoom to a marker if it's inside a marker cluster? I am changing the color of the markers if a user hovers over a html div. But if the marker is inside a cluster, the color won't change since the marker itself is not shown. Could anyone provide me with possible ways of solving this issue?
var allMarkers = [];
(function($) {
"use strict";
// Custom options for map
var options = {
zoom: 12,
mapTypeId: 'Styled',
disableDefaultUI: true,
panControl: true,
zoomControl: true,
scaleControl: true,
mapTypeControlOptions: {
mapTypeIds: ['Styled']
}
};
var styles = [{
stylers: [{
hue: "#cccccc"
}, {
saturation: -100
}]
}, {
featureType: "road",
elementType: "geometry",
stylers: [{
lightness: 100
}, {
visibility: "simplified"
}]
}, {
featureType: "road",
elementType: "labels",
stylers: [{
visibility: "on"
}]
}, {
featureType: "poi",
stylers: [{
visibility: "off"
}]
}];
var newMarker = null;
var markers = [];
// json for properties markers on map
var props = < ? php echo json_encode($map_flats); ? > ;
// custom infowindow object
var infobox = new InfoBox({
disableAutoPan: false,
maxWidth: 202,
pixelOffset: new google.maps.Size(-101, -285),
zIndex: null,
boxStyle: {
background: "url('images/infobox-bg.png') no-repeat",
opacity: 1,
width: "202px",
height: "245px"
},
closeBoxMargin: "28px 26px 0px 0px",
closeBoxURL: "",
infoBoxClearance: new google.maps.Size(1, 1),
pane: "floatPane",
enableEventPropagation: false
});
// function that adds the markers on ma
var addMarkers = function(props, map) {
$.each(props, function(i, prop) {
var latlng = new google.maps.LatLng(prop.position.lat, prop.position.lng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
id: +prop.id,
icon: new google.maps.MarkerImage(
'images/' + prop.markerIcon,
null,
null,
null
),
draggable: false,
animation: google.maps.Animation.DROP,
});
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
bounds.extend(markers[i].getPosition());
}
map.fitBounds(bounds);
allMarkers.push(marker); //Add it to allMarkers
var infoboxContent = '<div class="infoW">' +
'<div class="propImg">' +
'<img src="uploads/' + prop.image + '">' +
'<div class="propBg">' +
'</div>' +
'</div>' +
'<div class="paWrapper">' +
'<div class="propTitle">€' + prop.title + '</div>' +
'<div class="propAddress">' + prop.address + '</div>' +
'</div><br>' +
'<ul class="propFeat">' +
'<li><span class="fa fa-moon-o"></span> ' + prop.bedrooms + ' room(s)</li>' +
'<li><span class="icon-frame"></span> ' + prop.area + ' m<sup>2</sup></li>' +
'</ul>' +
'<div class="clearfix"></div>' +
'<div class="infoButtons">' +
'<a class="btn btn-sm btn-round btn-gray btn-o closeInfo">Close</a>' +
'<a target="_blank" href="single.php?id=' + prop.id + '" class="btn btn-sm btn-round btn-green viewInfo">View</a>' +
'</div>' +
'</div>';
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infobox.setContent(infoboxContent);
infobox.open(map, marker);
}
})(marker, i));
google.maps.event.addListener(map, "click", function(event) {
infobox.close();
});
$(document).on('click', '.closeInfo', function() {
infobox.open(null, null);
});
markers.push(marker);
});
//set style options for marker clusters (these are the default styles)
var mcOptions = {
styles: [{
height: 53,
url: "images/m1.png",
width: 53
}, {
height: 54,
url: "images/m1.png",
width: 54
}, {
height: 66,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m3.png",
width: 66
}, {
height: 78,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m4.png",
width: 78
}, {
height: 90,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m5.png",
width: 90
}],
gridSize: 50,
maxZoom: 14
}
//init clusterer with your options
var mc = new MarkerClusterer(map, markers, mcOptions);

The Cluster doesn't change color (yet ?). But I do have code that detects if the client hovers over his marker inside the cluster. So, now it logs a message onyour screen.
Look at the scripts I load. Google Maps, MarkerClustererPlus and a script containing data. Many locations (of photographs).
So, the marker of the client, is the marker in Central Park, New York, USA.
Try it out.
Any other idea how to visualize the detection? Like add a circle around the cluster, ... ?
<script src="http://maps.google.com/maps/api/js"></script>
<script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclustererplus/src/markerclusterer.js"></script>
<script src="https://googlemaps.github.io/js-marker-clusterer/examples/data.json"></script>
<script>
var client_index = 821; // Central Park, New York. Item 821 ( "photo_id": 586159 )
// We run through all the marker-objects inside the cluster; and we see if the marker object of our client marker is inside that array
function clientMarkerInCluster(allMarkers, markersInCluster) {
if(markersInCluster.indexOf( allMarkers[client_index] ) > 0) {
return true;
}
return false;
}
function initialize() {
var center = new google.maps.LatLng(40.480467417349345,-98.80444335937501);
var options = {
'zoom': 4,
'center': center,
'mapTypeId': google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), options);
var markers = [];
for(var key in data.photos) {
markers.push(new google.maps.Marker({
position: new google.maps.LatLng(data.photos[key].latitude, data.photos[key].longitude),
map: map,
title: key +' '+ data.photos[key].photo_title
})
);
}
var mcOptions = {gridSize: 50, maxZoom: 15};
var mc = new MarkerClusterer(map, markers, mcOptions);
google.maps.event.addListener(mc, "mouseover", function (c) {
if(clientMarkerInCluster(markers, c.markers_)) {
log('Client marker is inside the cluster');
}
});
// google.maps.event.addListener(mc, "mouseout", function (c) {});
}
google.maps.event.addDomListener(window, 'load', initialize);
// just a function to show a log on screen
function log(h) {
document.getElementById("log").innerHTML += h + "<br>";
}
</script>
<style>
#map {
height: 400px;
}
</style>
<div id="map"></div>
<div id="log"></div>

Related

Drawing lines from one marker to all other markers and open infowindows of each marker for click

Please click here for screen shot I have Google map which shows the relation between locations based on a condition. What I need is to draw lines from one marker to all other markers and open info windows of each marker for marker click. Can anybody suggest the way to perform the task?
var Markerss = {};
var linearray = [];
function initialize() {
var markers = [{ 'lat': '32.803', 'lon': '-96.7699', 'ExpertsCount': 2, 'Type': 'ofsite' }, { 'lat': '44.9542', 'lon': '-93.1139', 'ExpertsCount': 3, 'Type': 'onsite' }, { 'lat': '32.903', 'lon': '-150.0011', 'ExpertsCount': 5, 'Type': 'offsite' }, { 'lat': '61.85', 'lon': '-97.6501', 'ExpertsCount': 9, 'Type': 'onnsite' }];
var mapProp = {
center: new google.maps.LatLng(28.067768, 2.150065),
zoom: 5,
mapTypeId: 'terrain'
};
//var infowindow = new google.maps.InfoWindow({
// content: "Hello World!"
//});
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var icon = "";
for (i = 0; i <= markers.length; i++) {
var data = markers[i];
var myLatlng = new google.maps.LatLng(parseFloat(data.lat), parseFloat(data.lon));
if (data.Type == "offsite") {
icon = 'http://maps.google.com/mapfiles/ms/icons/green-dot.png';
}
else if (data.Type == "onsite") {
icon = 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png';
}
else {
icon = 'http://maps.google.com/mapfiles/ms/icons/red-dot.png';
}
var div = document.createElement('DIV');
div.innerHTML = '<div class="mapicon" id="dvIcon" style="background-image: url(images/mapiconsmall.png);" onmouseover="javascript:$(this).next().show()" onmouseout="javascript:$(this).next().hide()" ><div class="maindiv"style="width:24px;margin-top:3px; text-align:center;margin-left:1px;"><span style="font-weight:bolder;font-size:11px;"> ' + data.ExpertsCount + '</span></div></div><div id="dvExpName" style="display:none;z-index:1;position:absolute;">' + data.expName + '</div>';
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: i,
content: div,
flat: true,
anchor: RichMarkerPosition.MIDDLE,
icon: icon
});
Markerss[i] = marker;
//if (data.Type == "onsite") {
// Marker[i].setClickable = false;
//}
//else {
// Marker[i].setClickable = true;
//}
// Attaching a click event to the current marker
if (markers[i].Type != "onsite") {
google.maps.event.addListener(marker, 'click', function (event) {
if (linearray.length != 0)
{
for (i = 0; i < linearray.length; i++) {
linearray[i].setMap(null);
}
}
var lineSymbol = {
path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
scale: 3,
strokeColor: 'red'
};
for (j = 0; j < markers.length; j++) {
var marker = markers[j];
var lat = parseFloat(markers[j].lat);
var lon = parseFloat(markers[j].lon);
if (lat != event.latLng.lat()) {
var line = new google.maps.Polyline({
path: [{ lat: event.latLng.lat(), lng: event.latLng.lng() }, { lat: lat, lng: lon }],
icons: [{
icon: lineSymbol,
offset: '100%'
}],
strokeColor: 'red',
map: map
});
linearray.push(line);
}
var infoWindow = new google.maps.InfoWindow({
content: '<b><i>Expert Count:</i></b> <span>' + parseInt(markers[j].ExpertsCount) + '</span><br/><b><i>Expert Count:</i></b> <span>' + parseInt(markers[j].ExpertsCount) + '</span><br/>'
});
infoWindow.open(map, Markerss[j]);
}
});
}
}
}

Having Issue on Rendering Markers Info Windows By Side links Click

Can you please take a look at this demo and let me know why I am not able to render associated info windows on link clicks?
As you can see I already tried
$('#mapa').append('<li class=' + marker.id + '>' + data.markers[p].title + '</li>');
function showInfo(i) {
google.maps.event.trigger(locations[i], "click");
}
but I am getting
Uncaught ReferenceError: showInfo is not defined
when I click on the links!
Thanks
To be run from an HTML on click function, your showInfo function needs to be in the global context.
Also, if you look at the generated HTML you will see:
Barcelona
(-1 is not a valid index for the array)
And the locations array is empty, you never push the markers on to it.
When I fix all of those issues, the links work: updated fiddle
code snippet:
var map;
var locations = [];
function showInfo(i) {
google.maps.event.trigger(locations[i], "click");
}
$(document).ready(function() {
var latlng = new google.maps.LatLng(11.610707, 122.740089);
var myOptions = {
zoom: 12,
center: latlng,
disableDefaultUI: true,
scaleControl: true,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeControl: true,
draggableCursor: 'move',
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: ""
});
var data = {
"markers": [{
"latitude": 11.606503,
"longitude": 122.712637,
"title": "Copenhagen"
}, {
"latitude": 11.585988,
"longitude": 122.757084,
"title": "Barcelona"
}
]
};
locations.length = 0;
for (p = 0; p < data.markers.length; p++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[p].latitude, data.markers[p].longitude),
map: map,
draggable: false,
title: "marker " + p,
id: p
});
locations.push(marker);
$('#mapa').append('<li class=' + marker.id + '>' + data.markers[p].title + '</li>');
bindInfoWindow(marker, map, infowindow, data.markers[p].title);
}
function bindInfoWindow(marker, map, infowindow, strDescription) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(strDescription);
infowindow.open(map, marker);
});
}
});
#import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css');
#import url('http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css');
.container {
margin-top: 30px;
}
#map_canvas {
width: 100%;
height: 400px;
}
.highlighted {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script>
<script src="http://maps.google.com/maps/api/js"></script>
<ul id="mapa">
</ul>
<div class="container">
<div class="well">
<div id="map_canvas"></div>
</div>
</div>

Enabling and Disabling Dragging Markers by Checkbox

Can you please take a look at this Demo and let me know how I can make the markers Dragable when the check box is unchecked. Please be informed that all markers has their own associated checkbox it means that user can lock each marker which they want( Not all together). Initially all Markers are unchecked when they loaded to page . And finally I need to change the icon when the marker checked as lock.
var contentString = ' -- Lock <input name="your_name" value="your_value" type="checkbox">';
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({ content: "" });
var data = {
"markers": [{
"latitude": 11.606503,
"longitude": 122.712637,
"title": "Copenhagen"
}, {
"latitude": 11.585988,
"longitude": 122.757084,
"title": "Barcelona"
}
]
};
locations.length = 0;
for (p = 0; p < data.markers.length; p++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[p].latitude, data.markers[p].longitude),
map: map,
draggable: true,
title: "marker " + p,
id: p
});
bindMarker(marker);
bindInfoWindow(marker, map, infowindow, data.markers[p].title);
}
function bindMarker(marker) {
google.maps.event.addListener(marker, 'dragend', function () {
marker.setIcon('https://www.google.com/mapfiles/marker_green.png');
});
google.maps.event.addListener(marker, 'click', function () {
});
}
function bindInfoWindow(marker, map, infowindow, strDescription) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(strDescription + contentString);
infowindow.open(map, marker);
});
}
});
**
Update
function bindMarkerDrag(marker){
google.maps.event.addListener(checkbox, "click", function(){
draggable: false
//marker.setClickable(checkbox.checked);
});
}
then I add the
bindMarkerDrag(marker);
inside the loop Here is a Demo but not working
As I said in my comment, you need to add checkbox.checked = !marker.getDraggable(); in your marker click event listener. Not in the checkbox event listener.
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = !marker.getDraggable();
checkbox.addEventListener('click', function () {
marker.setDraggable(!this.checked);
});
Create the checbox
Check it or not if the marker is draggable or not
Manage the checkbox click event
You could something like:
google.maps.event.addListener(marker, 'mouseover', function(e){
marker.setDraggable(document.getElementById('checkbox').checked);
});
This will set the draggable property to false or true before you start dragging (if you put it on the dragstart event, you will have a slight delay.)
Replace the id checkbox with the one you need.
Check this code!! I have updated buildInfoWindow function to meet your requirements.
var map;
var locations = [];
var contentString = "--";
var infowindows = [];
$(document).ready(function () {
var latlng = new google.maps.LatLng(11.610707, 122.740089);
var myOptions = {
zoom: 12,
center: latlng,
disableDefaultUI: true,
scaleControl: true,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeControl: true,
draggableCursor: 'move',
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var data = {
"markers": [{
"latitude": 11.606503,
"longitude": 122.712637,
"title": "Copenhagen"
}, {
"latitude": 11.585988,
"longitude": 122.757084,
"title": "Barcelona"
}
]
};
locations.length = 0;
for (p = 0; p < data.markers.length; p++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[p].latitude, data.markers[p].longitude),
map: map,
draggable: true,
title: "marker " + p,
id: p
});
var infowindow = new google.maps.InfoWindow({ content: "" });
bindMarker(marker);
bindInfoWindow(marker, map, infowindow, data.markers[p].title, p);
bindMarkerDrag(marker);
}
function bindMarker(marker) {
google.maps.event.addListener(marker, 'dragend', function () {
marker.setIcon('https://www.google.com/mapfiles/marker_green.png');
});
}
function bindMarkerDrag(marker){
}
function bindInfoWindow(marker, map, infowindow, strDescription, p) {
google.maps.event.addListenerOnce(marker, 'click', function() {
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.addEventListener('click', function () {
marker.setDraggable(!this.checked);
});
var div = document.createElement('div');
div.innerHTML = strDescription + contentString + "<br /> Lock drag: ";
div.appendChild(checkbox);
infowindow.setContent(div);
infowindow.open(map, marker);
infowindows[p]= infowindow;
});
google.maps.event.addListener(marker, 'click', function() {
infowindows[p].open(map, marker);
});
}
});

Popup styling in google maps

i use markerclusterer and i show popup info like that: ( in my js file )
var infowindow = new google.maps.InfoWindow();
after that i use :
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(
'<div id="wrapper">'+
'<div id="header">'+data.photos[i].owner_name+':</div>'+
'<div id="content">'+data.photos[i].photo_title+'</div>'+
'</div>'
);
infowindow.open(map, marker);
}
})(marker, i));
in this case i can customize only that information, what i put in my box ( infowindow.setContent )
but how can i customize popup windows ?? if i want some border-radius on my popup...
i saw infobox plugin, but i can't to set it up ( for many popups - in FOR cicle )
$(function(){
function initialize() {
var styles = [[{
url: '../images/conv30.png',
height: 27,
width: 30,
anchor: [3, 0],
textColor: '#ff00ff',
textSize: 10
}, {
url: '../images/conv40.png',
height: 36,
width: 40,
anchor: [6, 0],
textColor: '#ff0000',
textSize: 11
}, {
url: '../images/conv50.png',
width: 50,
height: 45,
anchor: [8, 0],
textSize: 12
}]];
var center = new google.maps.LatLng(37.4419, -122.1419);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions:{
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
navigationControlOptions: {
style: google.maps.NavigationControlStyle.ANDROID
},
scaleControl: true
});
var infowindow = new google.maps.InfoWindow();
var style = 0;
var mcOptions = {gridSize: 50, maxZoom: 9, styles: styles[style]};
var imageUrl = 'http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=00FF00,FF00FF,000000&ext=.png';
var markerImage = new google.maps.MarkerImage(imageUrl, new google.maps.Size(24, 32));
var markers = [];
for (var i = 0; i < 100; i++) {
var dataPhoto = data.photos[i];
var title = data.photos[i].photo_title;
var latLng = new google.maps.LatLng(dataPhoto.latitude, dataPhoto.longitude);
var marker = new google.maps.Marker({
position: latLng,
title: title,
icon: markerImage
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(
'<div id="wrapper">'+
'<div id="header">'+data.photos[i].owner_name+':</div>'+
'<div id="content">'+data.photos[i].photo_title+'</div>'+
'</div>'
);
infowindow.open(map, marker);
}
})(marker, i));
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
});
i know that i must chang infowindow.setContent, but how...
help my plz with that.

Disable point-of-interest information window using Google Maps API v3

I have a custom map with an information bubble and custom markers. When I zoom into points of interest such as parks and universities appear and when I click an information window opens. How do I disable the info window?
My code is as follows:
var geocoder;
var map;
var infoBubble;
var dropdown = "";
var gmarkers = [];
var hostel_icon = new google.maps.MarkerImage('/resources/hostel_blue_icon.png',
new google.maps.Size(28,32),
new google.maps.Point(0,0),
new google.maps.Point(14,32));
var bar_icon = new google.maps.MarkerImage('/resources/bar_red_icon.png',
new google.maps.Size(28,32),
new google.maps.Point(0,0),
new google.maps.Point(14,32));
var icon_shadow = new google.maps.MarkerImage('/resources/myicon_shadow.png',
new google.maps.Size(45,32),
new google.maps.Point(0,0),
new google.maps.Point(12,32));
var customIcons = {
hostel: {
icon: hostel_icon,
shadow: icon_shadow
},
bar: {
icon: bar_icon,
shadow: icon_shadow
}
};
function initialize() {
var latlng = new google.maps.LatLng(12.82364, 26.29987);
var myMapOptions = {
zoom: 2,
center: latlng,
panControl: false,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.TERRAIN,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR},
navigationControlOptions: {style: google.maps.NavigationControlStyle.DEFAULT}
}
map = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);
infoBubble = new InfoBubble({
shadowStyle: 0,
padding: 0,
backgroundColor: 'rgb(57,57,57)',
borderRadius: 5,
arrowSize: 10,
borderWidth: 1,
maxWidth: 400,
borderColor: '#2c2c2c',
disableAutoPan: false,
hideCloseButton: true,
arrowPosition: 50,
backgroundClassName: 'phoney',
arrowStyle: 0
});
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml_2.php", function(data) {
var xml = parseXml(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var bar_name = markers[i].getAttribute("bar_name");
var hostel_name = markers[i].getAttribute("hostel_name");
var street = markers[i].getAttribute("street");
var city = markers[i].getAttribute("city");
var postcode = markers[i].getAttribute("postcode");
var country = markers[i].getAttribute("country");
var page = markers[i].getAttribute("page");
var map_photo = markers[i].getAttribute("map_photo");
var type = markers[i].getAttribute("type");
var category = markers[i].getAttribute("category");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = '<div class="infowindow"><div class="iwPhoto" style="width: 105px; height: 65px;">' + "' + "<a href='" + page + "'>" + hostel_name + "" + '</div><div class="iwCategory" style="height: 15px;">' + category + '</div><div class="iwCity" style="height: 29px;">' + "' + "<a href='" + page + "'><img src='/resources/arrow.png'/>" + '</div></div></div>';
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow,
title: bar_name
});
marker.bar_name = bar_name;
marker.category = category;
bindInfoBubble(marker, map, infoBubble, html, bar_name);
gmarkers.push(marker);
str = '<option selected> - Select a club - </option>';
for (j=0; j < gmarkers.length; j++){
str += '<option value="'+gmarkers[j].bar_name+'">'+gmarkers[j].bar_name+', '+gmarkers[j].category+'</option>';
}
var str1 ='<form name="form_city" action=""><select style="width:150px;" id="select_city" name="select_cityUrl" onchange="handleSelected(this);">';
var str2 ='</select></form>';
dropdown = str1 + str + str2;
}
document.getElementById("dd").innerHTML = dropdown;
});
}
function handleSelected(opt) {
var indexNo = opt[opt.selectedIndex].index;
google.maps.event.trigger(gmarkers[indexNo-1], "click");
}
function bindInfoBubble(marker, map, infoBubble, html) {
google.maps.event.addListener(marker, 'click', function() {
infoBubble.setContent(html);
infoBubble.open(map, marker);
google.maps.event.addListener(map, "click", function () {
infoBubble.close();
});
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser).parseFromString(str, 'text/xml');
}
}
function doNothing() {}
UPDATE Google Maps JavaScript API V3
You can set clickableIcons to false in MapOptions. This will keep the POI icons but disable the infowindows just as you want.
function initialize() {
var myMapOptions = { clickableIcons: false }
}
Further details here...
https://developers.google.com/maps/documentation/javascript/3.exp/reference#MapOptions
See the other answers for the clickable: false answer.
However, if you want it clickable, but no infowindow, call stop() on the event to prevent the infowindow from showing, but still get the location info:
map.addListener('click', function (event) {
// If the event is a POI
if (event.placeId) {
// Call event.stop() on the event to prevent the default info window from showing.
event.stop();
// do any other stuff you want to do
console.log('You clicked on place:' + event.placeId + ', location: ' + event.latLng);
}
}
For more info, see the docs.
Other option: completely remove the POI-icons, and not only the infoWindow:
var mapOptions = {
styles: [{ featureType: "poi", elementType: "labels", stylers: [{ visibility: "off" }]}]
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
You can do this by creating a styled map without labels for the points of interest.
This maintains the topography and other nice pieces of information on the map, but removes the markers.
var remove_poi = [
{
"featureType": "poi",
"elementType": "labels",
"stylers": [
{ "visibility": "off" }
]
}
]
map.setOptions({styles: remove_poi})
You could consider the following approach to disable POI Info Window:
function disablePOIInfoWindow(){
var fnSet = google.maps.InfoWindow.prototype.set;
google.maps.InfoWindow.prototype.set = function () {
};
}
Example
function initMap() {
var latlng = new google.maps.LatLng(40.713638, -74.005529);
var myOptions = {
zoom: 17,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
disablePOIInfoWindow();
}
function disablePOIInfoWindow(){
var fnSet = google.maps.InfoWindow.prototype.set;
google.maps.InfoWindow.prototype.set = function () {
alert('Ok');
};
}
google.maps.event.addDomListener(window, 'load', initMap);
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map_canvas {
height: 100%;
}
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<div id="map_canvas"></div>
The above example affects all the info windows, so if you need to disable only POI Info Window, then we could introduce flag to determine whether it is POI Info Window or not:
function disablePOIInfoWindow(){
var fnSet = google.maps.InfoWindow.prototype.set;
google.maps.InfoWindow.prototype.set = function () {
if(this.get('isCustomInfoWindow'))
fnSet.apply(this, arguments);
};
}
Example
function initMap() {
var latlng = new google.maps.LatLng(40.713638, -74.005529);
var myOptions = {
zoom: 17,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infowindow = new google.maps.InfoWindow({
content: ''
});
infowindow.set('isCustomInfoWindow',true);
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
disablePOIInfoWindow();
google.maps.event.addListener(map, 'click', function (event) {
infowindow.setContent(event.latLng.lat() + "," + event.latLng.lng());
infowindow.setPosition(event.latLng);
infowindow.open(map, this);
});
}
function disablePOIInfoWindow(){
var fnSet = google.maps.InfoWindow.prototype.set;
google.maps.InfoWindow.prototype.set = function () {
if(this.get('isCustomInfoWindow'))
fnSet.apply(this, arguments);
};
}
//google.maps.event.addDomListener(window, 'load', initMap);
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map_canvas {
height: 100%;
}
<div id="map_canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
Simply style the map to not show Points of Interest. This is simple and does not breach Google's terms of service.
eg
mapOpts = {
styles: [
{
featureType: "poi",
stylers: [
visibility: "off"
]
}
]
};
$("#my-map").gmap(mapOpts).on("init", function(evt, map){
// do stuff with the initialised map
});
If you want the data without getting the InfoWindow HTML showing at all, you simply have to re-work the prototype of google.maps.InfoWindow:
google.maps.InfoWindow.prototype.open = function () {
return this; //prevent InfoWindow to appear
}
google.maps.InfoWindow.prototype.setContent = function (content) {
if (content.querySelector) {
var addressHTML = content.querySelector('.address');
var address = addressHTML.innerHTML.replace(/<[^>]*>/g, ' ').trim();
var link = content.querySelector('a').getAttribute('href');
var payload = {
header: 'event',
eventName: 'place_picked',
data: {
name: content.querySelector('.title').innerHTML.trim(),
address: address,
link: link
}
};
console.log('emit your event/call your function', payload);
}
};
We can do it by handling clicks on poi, Google api has provided a way to detect clicks on POI as per this article
Based on article above Here is a simpler version of code that can be used to stop the clicks on POI
function initMap() {
map = new google.maps.Map(document.getElementById('map'), myOptions);
var clickHandler = new ClickEventHandler(map, origin);
}
var ClickEventHandler = function (map, origin) {
this.origin = origin;
this.map = map;
this.map.addListener('click', this.handleClick.bind(this));
};
ClickEventHandler.prototype.handleClick = function (event) {
//console.log('You clicked on: ' + event.latLng);
if (event.placeId) {
//console.log('You clicked on place:' + event.placeId);
// Calling e.stop() on the event prevents the default info window from
// showing.
// If you call stop here when there is no placeId you will prevent some
// other map click event handlers from receiving the event.
event.stop();
}
}