Google Maps API v3 Point of Interest with Custom Icons - google-maps

I have a page pulling in the schools, churches, and parks of my given area but I want to style the 3 POIs with 3 different icons. I searched Google and even all the docs but couldn't find the answer.
var map;
var infowindow;
function initialize() {
// Center of Map
var centerLatlng = new google.maps.LatLng(29.745376, -95.380125);
// Marker Icons Declaration
var icon = new google.maps.MarkerImage("smLinks-twitter.png", null, null, new google.maps.Point(41,47));
// Map Options
var myOptions = {
zoom: 16,
center: centerLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
icons: icon
};
// Draw the map
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Marker Icons Implementation
markers = new google.maps.Marker({
position: centerLatlng,
map: map,
title: 'Center of Map',
icon: icon
});
// Services: Places
var request = {
location: centerLatlng,
radius: 800,
types: ["school", "church", "park"]
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.search(request, callback);
} // function initialize()
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: icon
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
infowindow.open(map, this);
});
}

Please see my quick and dirty Demo. The idea is to use the place.types array to determine what kind of place you are trying to add to the map. I simplistically assigned a marker to the first item of this array (usually 2 or 3 items long), which may be something like:
["school", "establishment"]
In some cases, "university" comes before "school" so a "university" icon is used. You will want to refine the way you match types to icons, that is, give a priority for school or university? Perhaps iterate through the array looking for the right types. One place (general_contractor) is not in my list of icons, so the default pin marker is placed there. A "default" icon could be used if you checked if iconType in fact has or not the desired type. I'll leave these details to you =)
Here's the source I used for icons: https://sites.google.com/site/gmapsdevelopment/
function createMarker(place) {
var placeLoc = place.geometry.location;
var iconType = {};
iconType['school'] = "http://maps.google.com/mapfiles/kml/pal2/icon2.png";
iconType['church'] = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
iconType['park'] = "http://maps.google.com/mapfiles/kml/pal2/icon12.png";
iconType['university'] = "http://maps.google.com/mapfiles/kml/pal2/icon14.png";
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: iconType[place.types[0]]
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
infowindow.open(map, this);
});
}
Alternatively, use a switch statement:
function createMarker(place) {
var placeLoc = place.geometry.location;
var iconUrl;
switch (place.types[0]) {
case 'school':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon2.png";
break;
case 'church':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
break;
case 'park':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon12.png";
break;
case 'university':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon14.png";
break;
default:
iconUrl = "http://maps.google.com/mapfiles/kml/pal4/icon39.png";
}
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: iconUrl
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
infowindow.open(map, this);
});
}

Related

Google maps showing same info window for all markers

I'm looping over an array of properties and create multiple markers on a google map. I'd like each of these markers to have their own info window content. My code looks right to me but it's not working. Can somebody point me in the right direction.
Here is my current code
for (var i = startRow; i < AMdata.property.length; i++) {
var propertyDetails = AMdata.property[i];
var latLng = new google.maps.LatLng(propertyDetails.LATITUDE, propertyDetails.LONGITUDE);
bounds.extend(latLng);
var marker = new google.maps.Marker({
position: latLng,
title: propertyDetails.NAME,
icon: outIcon
});
// 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('<div class="infowindow"><div class="panel-image listing-img"><img style="max-height:115px;" width="100%" src="' + propertyDetails.THUMBNAIL + '"></div><p>' + propertyDetails.NAME + '</p></div>');
infowindow.open(map, this);
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
map.fitBounds(bounds);
try this way :
for (var i = startRow; i < AMdata.property.length; i++) {
var propertyDetails = AMdata.property[i];
var latLng = new google.maps.LatLng(propertyDetails.LATITUDE, propertyDetails.LONGITUDE);
bounds.extend(latLng);
var marker = new google.maps.Marker({
position: latLng,
title: propertyDetails.NAME,
icon: outIcon
});
// add an event listener for this marker
k = markers.push(marker);
google.maps.event.addListener(markers[k-1] , 'click', function(propertyDetail) {
// assuming you have some content in a field called Field123
infowindow.setContent('<div class="infowindow"><div class="panel-image listing-img"><img style="max-height:115px;" width="100%" src="' + propertyDetails.THUMBNAIL + '"></div><p>' + propertyDetails.NAME + '</p></div>');
infowindow.open(map, this);
});
}
var markerCluster = new MarkerClusterer(map, markers);
map.fitBounds(bounds);

converting coordinates to address v3 google api

I am not able to convert my coordinates to address ...
Almost got adjusting function below ... but how many markers are, just creating only the last marker ...:
My function to create markers:
function createMarker(point,info,map) {
var iconURL = 'img/pata.png';
var iconSize = new google.maps.Size(32,34);
var iconOrigin = new google.maps.Point(0,0);
var iconAnchor = new google.maps.Point(15,30);
var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor);
var marker = new google.maps.Marker({
position : point,
html : info,
map : map,
icon: myIcon
});
var infowindow = new google.maps.InfoWindow({
content: "Dispositivo: " + info + "<br> Endereço: " + point
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,this);
});
}
My function to create bookmarks:
Function that was trying to adapt (just wanted to send the coordinates and he convert to address and leave the infowindow when clicking)
function codeLatLng() {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
could,
but do not know how to get the addresses separately to list ...
but got the click infowindow:
function createMarkerAtual(point,info,dt,map) {
var iconURL = 'img/dog2.png';
var iconSize = new google.maps.Size(45,45);
var iconOrigin = new google.maps.Point(0,0);
var iconAnchor = new google.maps.Point(15,30);
var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor);
var marker = new google.maps.Marker({
position : point,
html : info,
map : map,
icon: myIcon
});
google.maps.event.addListener(marker, 'click', function() {
endereco(info,this.position);
infowindow.open(map,this);
});
}
function endereco(info,point){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': point}, function(results, status){
if (status == google.maps.GeocoderStatus.OK){
infowindow.setContent('<b>Coordenadas: </b>' + point + '<br><b>Dispositivo:</b> ' + info + '<br><b>Endereço: </b>' + results[0].formatted_address);
} else {
}
});
}

Custom icon Google Maps V3

This map works fine, I just have one trouble.
I need to create a different icon for each marker.
I really don't have lucky with my codes ^^
how do I?
var side_bar_html = "";
var gmarkers = [];
var map = null;
/**
*map
**/
var point = new google.maps.LatLng(-23.421409,-51.936722);
var marker = createMarker(point,"This place",contentString0)
var point = new google.maps.LatLng(-23.421409,-51.934722);
var marker = createMarker(point,"This place",contentString1)
var point = new google.maps.LatLng(-23.421409,-51.932722);
var marker = createMarker(point,"This place",contentString2)
//put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = side_bar_html;
}
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(150,50)
});
// This function picks up the click and opens the corresponding info window
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
// A function to create the marker and set up the event window function
function createMarker(latlng, name, html) {
var contentString = html;
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);
});
gmarkers.push(marker);
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';
}
In your marker constructor add an icon property with the icons URL to the anonymous object you're passing:
var marker = new google.maps.Marker({
position: latlng,
map: map,
icon:(image URL here),
zIndex: Math.round(latlng.lat()*-100000)<<5
});
https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions
(Send via mobile, sorry if layout is bad).

Infobubble only displaying content from last marker added via PHP/Mysql with tabs

I've been trying to get Infobubbles to work with 2 tabs on a map populated with markers from a Mysql database with an ajax call.
The problem is that every infobubble is displaying the same content in the tabs, from the last marker added. Here is the entire js script, any help is greatly appreciated.
var script = '<script type="text/javascript" src="http://google-maps-' +
'utility-library-v3.googlecode.com/svn/trunk/infobubble/src/infobubble';
script += '.js"><' + '/script>';
document.write(script);
var customIcons = {
free: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
animation: google.maps.Animation.DROP,
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
paid: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
animation: google.maps.Animation.DROP,
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
// End Custom Icons
var map, infoBubble;
function initialize() {
var myOptions = {
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'),
myOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position)
{
var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var marker = new google.maps.Marker({
map: map,
position: pos,
icon:'images/markers/you.png',
animation: google.maps.Animation.DROP,
title: 'Your Location.'
});
infoBubble = new InfoBubble({
maxWidth: 300,
borderRadius: 10,
borderWidth: 1,
borderColor: '#2c2c2c',
shadowStyle: 1
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.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("name");
var address = markers[i].getAttribute("address");
var citystate = markers[i].getAttribute("citystate");
var phone = markers[i].getAttribute("phone");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + citystate + "<br/>" + phone; //html inside InfoWindow
var description = "<b>" + name + "</b> <br/>" + address + "<br/>" + citystate + "<br/>" + phone; //html inside InfoWindow
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow,
animation: google.maps.Animation.DROP
});
bindInfoWindow(marker, map, infoBubble, html, description);
}
infoBubble.addTab('Tab 1', html);
infoBubble.addTab('Tab 2', description);
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infoBubble = new google.maps.infoBubble(options);
map.setCenter(options.position);
}
// Additional functions related to the DB Listing function
function bindInfoWindow(marker, map, infoBubble, html) {
google.maps.event.addListener(marker, 'click', function() {
//infoBubble.setContent(html);
infoBubble.open(map, marker);
});
}
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, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
function addTab() {
var title = document.getElementById('tab-title').value;
var content = document.getElementById('tab-content').value;
if (title != '' && content != '') {
infoBubble.addTab(title, content);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
The code that sets the content depending on the marker is commented out:
//infoBubble.setContent(html);
Therefor the content of the last infowindow created is always displayed.
working example, including the tab content
Q problem in google map information window on marker click when click on next marker it opens the info window with tabs but when again click on next marker then it oepns the info window with tabs but the prevoius tabs did not closed herte is the code
google.maps.event.addListener(marker, 'click', (function(marker, contentString) {
return function() {
infoBubble.addTab('Tab 1', contentString);
infoBubble.addTab('Tab 2', contentString12);
infoBubble.setContent(contentString);
infoBubble.close('Tab 1', contentString);
infoBubble.open(map,marker);

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