Google Maps API infobox - click one infobox and close the others - google-maps

I am using infobox plugin to create multiple markers with different content - http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/infobox-basic.html
Also I am using the markerclusterer plugin to handle too many markers in the same region - http://gmaps-utility-library-dev.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer.js
All the markers are showing correctly and clustering is working perfect.
The problem. My question is how could I just click one infobox and close the others, and if you select a different one it would open that and close the others.
Here is my current code [updated]: http://jsfiddle.net/henrichro/mqrrA/
<script type="text/javascript" src="<?php echo get_bloginfo('template_directory'); ?>/js/markerclusterer.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox.js"></script>
<script type="text/javascript">
// build portfolios list
var portfolios = [
<?php
global $post;
$type = 'portfolio';
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$my_id = get_the_ID();
$my_title = get_the_title($my_id);
$my_permalink = get_permalink($my_id);
$site_type = get_the_terms($my_id,'type');
if ( $site_type ) {
foreach ( $site_type as $st ) {
if ( $st->slug == 'cultural' ) {
$icon_type = get_bloginfo('template_directory').'/img/icon_blue.png';
} elseif ( $st->slug == 'natural' ) {
$icon_type = get_bloginfo('template_directory').'/img/icon_green.png';
} else {
$icon_type = get_bloginfo('template_directory').'/img/icon_mixed.png';
}
}
} else {
$icon_type = "marker.setIcon(null)";
}
if (get_post_meta($post->ID,'_my_meta',TRUE)) :
$key_meta = get_post_meta($post->ID,'_my_meta',TRUE);
endif;
if($key_meta[coord_lat] && $key_meta[coord_long]):
$lat = $key_meta[coord_lat];
$long = $key_meta[coord_long];
else :
$lat = '';
$long = '';
endif;
?>
['<?php echo $my_title; ?>', <?php echo $lat; ?>, <?php echo $long; ?>, '<?php echo $icon_type; ?>', '<?php echo $my_permalink; ?>'],
<?php
endwhile;
}
//wp_reset_query();
?>
];
function initialize() {
// pointing the center of the universe
var latlng = new google.maps.LatLng(0, 0);
// setting the map options
var myMapOptions = {
zoom: 2,
center: latlng,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
// creating the map
var map = new google.maps.Map(document.getElementById("googlemap"),myMapOptions);
// list of the coordinates/positions
var latlnglist = []
// bounding
var bounds = new google.maps.LatLngBounds();
var markers = [],
marker;
// building markers from portfolio list
for (i = 0; i < portfolios.length; i++) {
// marker position
var position = new google.maps.LatLng(portfolios[i][1], portfolios[i][2]);
// extending position into list
latlnglist.push(position);
bounds.extend(position);
// special/custom icon for the marker
var image = portfolios[i][3];
// marker options
var markerOptions = {
position: position,
icon: image,
map: map
}
// creating the marker
marker = new google.maps.Marker(markerOptions);
// creating list of markers
markers.push(marker);
// creating the div for the infobox
var boxText = document.createElement("div");
// infobox options
var infoboxOptions = {
content: boxText
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-140, 0)
,zIndex: null
,boxStyle: {
background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/examples/tipbox.gif') no-repeat"
,opacity: 0.75
,width: "280px"
}
,closeBoxMargin: "10px 2px 2px 2px"
,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
};
// infobox css styles
boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: white; padding: 5px; z-index: 9999";
// infobox content
boxText.innerHTML = "<a href='"+ portfolios[i][4] +"'>"+ portfolios[i][0] +"</a><br><br>Test";
// creating the infobox
markers[i].infobox = new InfoBox(infoboxOptions);
// marker action (click)
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
//infobox.setContent(portfolios[i][0]);
markers[i].infobox.close();
markers[i].infobox.open(map, this);
}
})(marker, i));
}
// defining cluster options
var clusterOptions = {
gridSize: 50,
maxZoom: 15
}
// creating the cluster object
var markerCluster = new MarkerClusterer(map, markers, clusterOptions);
var bounds = new google.maps.LatLngBounds();
for (var i = 0, len = latlnglist.length; i < len; i++) {
bounds.extend(latlnglist[i]);
}
map.fitBounds(bounds);
return markers;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

Not sure how much this helps but my solution was to only use one infowindow. For example, the listener I use for clicking map markers is:
google.maps.event.addListener(marker999, "click", function() {
infowindow.close();
infowindow.setContent('contenthere');
infowindow.open(map, marker999);
});
The above code is written for each marker I have displayed (with the unique marker ID and the specific infowindow content for that marker). Infobox should work similarly.
Working example: http://jsfiddle.net/PwJdM/

I've replaced a little bit the marker action (click) part with this:
// marker action (click)
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
for ( h = 0; h < markers.length; h++ ) {
markers[h].infobox.close();
}
markers[i].infobox.open(map, this);
}
})(marker, i));
Inspired from here: google maps api infobox plugin and multiple markers (almost the same code, removed some unnecesary lines in my case).
Updated the working example on jsfiddle: http://jsfiddle.net/henrichro/mqrrA/

it might be a late answer but I resolved this by triggering a custom event and adding a listener on my boxes. Worked fine for me.
// just before you open your infobox
marker.addListener('click', function () {
google.maps.event.trigger(map, "closeAllInfoboxes");
infobox.open(marker.get('map'), marker);
});
//Add this event listener to your infobox
google.maps.event.addListener(map, 'closeAllInfoboxes', function () {
infobox.close();
});`

Related

Google map : PHP - Map marker bounce when clicking on button

Stuck on the map marker bounce.I am showing a multiple location using marker on map dynamically. When i click on event button(Map icon), on map same location bounce.
Sharing my code:
Here is my view
What i want that, When i am clicking on map icon in Activity same marker on map with lat-long will bounce.Same for all.
Here is my a href link for map
<i class="fa fa-map-marker" aria-hidden="true"></i>
// here i gt latitude and longitude
Google map script:
<script>
function initMap() {
window.map = new google.maps.Map(document.getElementById('googleMap'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
<?php
if($listactivity !="" && count($listactivity)>0){
foreach($listactivity as $location){ ?>
var location = new google.maps.LatLng({{ $location->latitude }}, {{ $location->longitude }});
var marker = new google.maps.Marker({
position: location,
map: map
});
bounds.extend(marker.position);
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
}));
<?php }} ?>
map.fitBounds(bounds);
var listener = google.maps.event.addListener(map, "idle", function () {
if (map.getZoom() > 16) map.setZoom(16);
google.maps.event.removeListener(listener);
});
}
</script>
How can i do it dynamically.
This is how I do:
// Pushing all markers to an object with an ID, after the end of your php for loop, with the current loop variable, in this case use for cycle instead of foreach
<script>
var markers = [];
function initMap() {
window.map = new google.maps.Map(document.getElementById('googleMap'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
<?php
if($listactivity !="" && count($listactivity)>0){
for ($i = 0; $i < count($listactivity); $i++){ ?>
var currentIndex = {{ $i }}
var location = new google.maps.LatLng({{ $listactivity[$i]->latitude }}, {{ $listactivity[$i]->longitude }});
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push({id: currentIndex, marker: marker});
bounds.extend(marker.position);
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
}));
<?php }} ?>
map.fitBounds(bounds);
var listener = google.maps.event.addListener(map, "idle", function () {
if (map.getZoom() > 16) map.setZoom(16);
google.maps.event.removeListener(listener);
});
}
// Setting up click event
$('.markericon').on('click', function () {
var markerId = $(this).attr('data-id');
var result = $.grep(markers, function (e) {
if (e.id == markerId) {
return e;
}
});
// Trigger marker click event, or bounce effect.
google.maps.event.trigger(result[0].marker, 'click');
});
</script>
// Adding a markericon class, and storing the location's marker id in a data attribute
// e.g <a href="#" class="markericon" data-id="1">

Google Maps Api V3 how to create different marker icons (no xml used)

We pull data from a mysql database to create a google map that contains around 150 markers. The script is rather old. Some years ago we opted for a non-xml solution. While this might not be perfect, it works pretty good. We have three types of locations and would like to use different marker icons for that. I'm not sure how to do that. Here is the script:
var icon = new google.maps.MarkerImage("/icons/mm_20_red.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var title = "Für mehr Infos bitte klicken!";
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info, type) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map,
title: title
});
var popup = new google.maps.InfoWindow({
content: info
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(50.607685, 10.688126),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.LARGE
}
});
<?
$query = mysql_query("SELECT * FROM markers");
while ($row = mysql_fetch_array($query)) {
$name = $row['name'];
$vorname = $row['vorname'];
$lat = $row['lat'];
$lon = $row['lng'];
$address = $row['address'];
$strasse = $row['strasse'];
$plz = $row['plz'];
$ort = $row['ort'];
$telephone = $row['telephone'];
$email = $row['email'];
$image = $row['image'];
$type = $row['type'];
if ($row['image'] != "") {
echo("addMarker($lat, $lon,'<img style=\"float:left; margin: 0px 5px 0px 0px\" src=\"$image\" width=\"80\" height=\"80\" /><b>$name, $vorname ($type) </b><br/>$strasse<br/>$plz $ort<br/>$telephone<br/>$email');");
} else
echo("addMarker($lat, $lon,'<b>$name, $vorname ($type) </b><br/>$strasse<br/>$plz $ort<br/>$telephone<br/>$email');");
}
?>
}
So, we fetch all the data in the db and echo them. If there's an image, we show that in the popup. If not, we skip it. We have one additional column "type". We would like to use different markers for those type. Let's say a red one for type A, a blue one for type B, and a green one for type C. How could we do that?
Thanks!
You can select the color of the icon based on the type and use the image of that color as the marker icon.
function addMarker(lat, lng, info, type) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
// Added code starts here
var color = "black";
if(type == "A")
{
color = "red";
}
else if(type == "B")
{
color = "blue";
}
else if(type == "C")
{
color = "green";
}
var imageicon = "http://maps.google.com/mapfiles/ms/icons/" + color + ".png";
// Added code ends here
var marker = new google.maps.Marker({
position: pt,
icon: imageicon, //replace icon with imageicon
map: map,
title: title
});
var popup = new google.maps.InfoWindow({
content: info
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
currentPopup = null;
});
}

Google maps adding streetview to each infowindow

I would like to add streetview to each infowindow but I can't figure out how to integrate the code. I tried putting the code where the comments are set and that works half. Still have to learn a lot about programming.
html += '<div id="content" style="width:200px;height:200px;"></div>';
var pano = null;
google.maps.event.addListener(infoWindow, 'domready', function () {
if (pano != null) {
pano.unbind("position");
pano.setVisible(false);
}
pano = new google.maps.StreetViewPanorama(document.getElementById("content"), {
navigationControl: true,
navigationControlOptions: { style: google.maps.NavigationControlStyle.ANDROID },
enableCloseButton: false,
addressControl: false,
linksControl: false
});
pano.bindTo("point", marker);
pano.setVisible(true);
});
I'm using this code:
function load() {
var map = new google.maps.Map(document.getElementById("map_canvas"), {
center: new google.maps.LatLng(41.640078, -102.669433),
zoom: 3,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
downloadUrl("mymap.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 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/>" + point;
// comment *** streetview here ****
var marker = new google.maps.Marker({
map: map,
position: point
});
bindInfoWindow(marker, map, infoWindow, html);
}
});}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
Working example using DOM elements (rather than using string content):
// this variable will collect the html which will eventually be placed in the side_bar
var side_bar_html = "";
// arrays to hold copies of the markers used by the side_bar
var gmarkers = [];
// global "map" variable
var map = null;
var sv = new google.maps.StreetViewService();
var clickedMarker = null;
var panorama = null;
// Create the shared infowindow with three DIV placeholders
// One for a text string, oned for the html content from the xml, one for the StreetView panorama.
var content = document.createElement("DIV");
var title = document.createElement("DIV");
content.appendChild(title);
var streetview = document.createElement("DIV");
streetview.style.width = "200px";
streetview.style.height = "200px";
content.appendChild(streetview);
var htmlContent = document.createElement("DIV");
content.appendChild(htmlContent);
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(150, 50),
content: content
});
// 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,
title: name,
zIndex: Math.round(latlng.lat() * -100000) << 5
});
marker.myHtml = html;
google.maps.event.addListener(marker, "click", function() {
clickedMarker = marker;
sv.getPanoramaByLocation(marker.getPosition(), 50, processSVData);
// openInfoWindow(marker);
});
// save the info we need to use later for the side_bar
gmarkers.push(marker);
// add a line to the side_bar html
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + '<\/a><br>';
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
function processSVData(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
var marker = clickedMarker;
openInfoWindow(clickedMarker);
if (!!panorama && !!panorama.setPano) {
panorama.setPano(data.location.pano);
panorama.setPov({
heading: 270,
pitch: 0,
zoom: 1
});
panorama.setVisible(true);
google.maps.event.addListener(marker, 'click', function() {
var markerPanoID = data.location.pano;
// Set the Pano to use the passed panoID
panorama.setPano(markerPanoID);
panorama.setPov({
heading: 270,
pitch: 0,
zoom: 1
});
panorama.setVisible(true);
});
}
} else {
openInfoWindow(clickedMarker);
title.innerHTML = clickedMarker.getTitle() + "<br>Street View data not found for this location";
htmlContent.innerHTML = clickedMarker.myHtml;
panorama.setVisible(false);
// alert("Street View data not found for this location.");
}
}
function initialize() {
// Create the map
// No need to specify zoom and center as we fit the map further down.
map = new google.maps.Map(document.getElementById("map_canvas"), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false
});
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
// Read the data from example.xml
// downloadUrl("example.xml", function(doc) {
var xmlDoc = xmlParse(xmlData);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat, lng);
var html = markers[i].getAttribute("html");
var label = markers[i].getAttribute("label");
// create the marker
var marker = createMarker(point, label, html);
bounds.extend(point);
}
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = side_bar_html;
// Zoom and center the map to fit the markers
map.fitBounds(bounds);
// });
}
// Handle the DOM ready event to create the StreetView panorama
// as it can only be created once the DIV inside the infowindow is loaded in the DOM.
var pin = new google.maps.MVCObject();
google.maps.event.addListenerOnce(infowindow, "domready", function() {
panorama = new google.maps.StreetViewPanorama(streetview, {
navigationControl: false,
enableCloseButton: false,
addressControl: false,
linksControl: false,
visible: true
});
panorama.bindTo("position", pin);
});
// Set the infowindow content and display it on marker click.
// Use a 'pin' MVCObject as the order of the domready and marker click events is not garanteed.
function openInfoWindow(marker) {
title.innerHTML = marker.getTitle();
htmlContent.innerHTML = marker.myHtml;
pin.set("position", marker.getPosition());
infowindow.open(map, marker);
}
// This Javascript is based on code provided by the
// Community Church Javascript Team
// http://www.bisphamchurch.org.uk/
// http://econym.org.uk/gmap/
// from the v2 tutorial page at:
// http://econym.org.uk/gmap/basic3.htm
google.maps.event.addDomListener(window, 'load', initialize);
var xmlData = '<markers> <marker lat="43.65654" lng="-79.90138" html="Some stuff to display in the<br>First Info Window" label="Marker One" /> <marker lat="43.91892" lng="-78.89231" html="Some stuff to display in the<br>Second Info Window" label="Marker Two" /> <marker lat="43.82589" lng="-79.10040" html="Some stuff to display in the<br>Third Info Window" label="Marker Three" /></markers> ';
/**
* Parses the given XML string and returns the parsed document in a
* DOM data structure. This function will return an empty DOM node if
* XML parsing is not supported in this browser.
* #param {string} str XML string.
* #return {Element|Document} DOM.
*/
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
html,
body {
height: 100%;
}
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<!-- you can use tables or divs for the overall layout -->
<table border="1">
<tr>
<td>
<div id="map_canvas" style="width: 550px; height: 450px"></div>
</td>
<td valign="top" style="width:150px; text-decoration: underline; color: #4444ff;">
<div id="side_bar"></div>
</td>
</tr>
</table>

Custom Google Maps Markers and Infowindow

I want to create a google map in which the markers I want to show are coming from database and the infowindow data comes from database as well which is different for each marker obviously. Code that I am using now is:
<script type="text/javascript">
var delay = 100;
var infowindow = new google.maps.InfoWindow();
var latlng = new google.maps.LatLng(21.0000, 78.0000);
var mapOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var bounds = new google.maps.LatLngBounds();
function geocodeAddress(address, next) {
geocoder.geocode({address:address}, function (results,status){
if (status == google.maps.GeocoderStatus.OK) {
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
createMarker(address,lat,lng);
}else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
nextAddress--;
delay++;
} else {}
}
next();
});
}
function createMarker(add,lat,lng) {
var contentString = add;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
bounds.extend(marker.position);
}
<?php
global $wpdb;
$results = $wpdb->get_results("select * from $wpdb->postmeta where meta_value = 'Canada'");
$num_rows = $wpdb->num_rows;
?>
var locations = [
<?php foreach ($results as $doc_meta_data) {
echo "'";// . $doc_meta_data->meta_id;
echo get_post_meta($doc_meta_data->post_id, 'address', true).",";
echo get_post_meta($doc_meta_data->post_id, 'state', true).",";
echo get_post_meta($doc_meta_data->post_id, 'country', true);
echo "',";
}?>
];
var nextAddress = 0;
function theNext() {
if (nextAddress < locations.length) {
setTimeout('geocodeAddress("'+locations[nextAddress]+'",theNext)', delay);
nextAddress++;
} else {
map.fitBounds(bounds);
}
}
theNext();
</script>
How can I add make each markers info dynamic ie, it should come from the admin panel.
URL:http://intigateqatar.com/ozone/find-a-doc/
You need to create infowindow in createMarker() function in order infowindows to be unique for each marker:
function createMarker(add,lat,lng) {
var contentString = add;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map
});
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
bounds.extend(marker.position);
}
The InfoWindow's on the linked page have dynamic content(currently the address), I guess you don't need the address as content.
Currently the locations-array has the following structure.
[address1,address2,address3,....]
Request the infos and create an array with this structure:
[[address1,info1],[address2,info2],[address3,info3],....]
Change geocodeAddress() into the following:
function geocodeAddress(details, next) {
//details[0] is the address, pass it as address-property to geocode
geocoder.geocode({address:details[0]}, function (results,status){
if (status == google.maps.GeocoderStatus.OK) {
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
//details[1] is the info fetched from the DB,
//pass it as add-argument to createMarker()
createMarker(details[1],lat,lng);
}else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
nextAddress--;
delay++;
} else {}
}
next();
});
}
Note: echoing the single variables is not a good idea , a single-quote in either address(or the info) will result in a script-error(and there are more critical characters, e.g. linebreaks).
Populate a PHP-array with the values and use json_encode() to print the array.

Display Number on Marker for Google Maps

All,
I've got the following code to display my markers on my maps:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func
} else {
window.onload = function() {
oldonload();
func();
}
}
}
var map,
infowin=new google.maps.InfoWindow({content:'moin'});
function loadMap()
{
map = new google.maps.Map(
document.getElementById('map'),
{
zoom: 12,
mapTypeId:google.maps.MapTypeId.ROADMAP,
center:new google.maps.LatLng(<?php echo $_SESSION['pav_event_latitude']; ?>,
<?php echo $_SESSION['pav_event_longitude']; ?>)
});
addPoints(myStores);
}
function addPoints( points )
{
var bounds=new google.maps.LatLngBounds();
for ( var p = 0; p < points.length; ++p )
{
var pointData = points[p];
if ( pointData == null ) {map.fitBounds(bounds);return; }
var point = new google.maps.LatLng( pointData.latitude, pointData.longitude );
bounds.union(new google.maps.LatLngBounds(point));
createMarker( point, pointData.html );
}
map.fitBounds(bounds);
}
function createMarker(point, popuphtml)
{
var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
var marker = new google.maps.Marker(
{
position:point,
map:map
}
);
google.maps.event.addListener(marker, 'click', function() {
infowin.setContent(popuphtml)
infowin.open(map,marker);
});
}
function Store( lat, long, text )
{
this.latitude = lat;
this.longitude = long;
this.html = text;
}
var myStores = [<?php echo $jsData;?>, null];
addLoadEvent(loadMap);
</script>
This works great. However I'm trying to say add a number over the marker so that people can relate the number on my site with the marker in Google Maps. How can I go about creating the number over top of my markers (on top of the actual icon and not in an information bubble)?
Any help would be greatly appreciate! Thanks in advance!
EDIT: This API is now deprecated, and I can no longer recommend this answer.
You could use Google's Charts API to generate a pin image.
See: http://code.google.com/apis/chart/infographics/docs/dynamic_icons.html#pins
It'll make and return an image of a marker from the parameters you specify. An example usage would be: https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=2|FF776B|000000
To implement it into your Google Map, it can be added into the new Marker() code:
var number = 2; // or whatever you want to do here
var marker = new google.maps.Marker(
{
position:point,
map:map,
icon:'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+number+'|FF776B|000000',
shadow:'https://chart.googleapis.com/chart?chst=d_map_pin_shadow'
}
);
EDIT:
For quite some time now, map markers have an option called label available.
var marker = new google.maps.Marker({
position:point,
map:map,
label: "Your text here."
});
Labels themselves have few options to play with. You can read more about it here.
Original answer
Here is a service similar to one described by Rick - but still active and you can upload your own marker image.
Service is no longer available.