Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Google Maps and Markers Update

DZone's Guide to

Google Maps and Markers Update

· Web Dev Zone
Free Resource

Prove impact and reduce risk when rolling out new features. Optimizely Full Stack helps you experiment in any application.

Edit: Nice, it looks like I had already posted a follow-up. Maybe the readers of the blog entries missed it (I did!). But anyway, you can see yet another post on this subject here. Note to self: don't blog before coffee.

Last December I blogged a simple example of using custom markers in Google Maps. One of the issues folks ran into with my code was that it made use of Google's Geocoding service to translate addresses into latitude/longitude points. This works great--to a point. If you have more than 10 (or 11, never sure about this) then Google will block your Geocoding results. The fix is pretty simple, but since folks had issues with this I thought I'd describe it in more detail.

In the demo I used before I had an array of data. The data included the addresses I wanted to geocode. What I did was add a simple console message to my code so I could see the result from Google's geocoding service:

console.log(results[0].geometry.location.lat()+','+results[0].geometry.location.lng(),mapData.title);

Once I had that, I then modified my data to include the information:

var data = [
{pos:{lat:43.219778,lng:-87.926058},address:'1340 West Towne Square Road Mequon WI 53092',title:'Center for Diagnostic Imaging-Mequon',type:'lab'},
{pos:{lat:43.0622043,lng:-88.0475662},address:'2445 North Mayfair Road Wauwatosa WI 53226',title:'Center for Diagnostic Imaging-Wauwatosa',type:'lab'},
{pos:{lat:43.040085,lng:-88.0252289},address:'9200 W. Wisconsin Ave. Milwaukee WI 53227',title:'Clinical Cancer Center Pharmacy',type:'pharmacy'},
{pos:{lat:43.040085,lng:-88.0252289},address:'W180 N8085 Town Hall Road Menomonee Falls WI 53051',title:'Community Memorial Hospital',type:'hospital'}
];

Now, you may be wondering how this would work on a page that already has the bug I was talking about. It should still give you data up to 10 or 11 items. You could then temporarily remove data so that, when you run the page again, the rest are processed. If that doesn't make sense, you can also consider using a simple web app. This web app lets you drag a map around and then click for the data. This will be slow, but once you've done it you never need to do it again. Finally, you could also build a server-side application to do this for you, in chunks over time, and let it run until complete.

The point is, you have different ways to Geocode those addresses. Once you've done so, your code then gets simpler than what I had in the previous example. Here is the new loop:

  data.forEach(function(mapData,idx) {
		
		var marker = new google.maps.Marker({
			map: map, 
			position: new google.maps.LatLng(mapData.pos.lat,mapData.pos.lng),
			title: mapData.title,
			icon: getIcon(mapData.type)
		});
		var contentHtml = "<div style='width:300px;height:200px'><h3>"+mapData.title+"</h3>"+mapData.address+"</div>";
		var infowindow = new google.maps.InfoWindow({
			content: contentHtml
		});
		google.maps.event.addListener(marker, 'click', function() {
		  infowindow.open(map,marker);
		});
		marker.locid = idx+1;
		marker.infowindow = infowindow;
		markers[markers.length] = marker;

		var sideHtml = '<p class="loc" data-locid="'+marker.locid+'"><b>'+mapData.title+'</b><br/>';
			 sideHtml += mapData.address + '</p>';
			 $("#locs").append(sideHtml); 

		//Are we all done? Not 100% sure of this
		if(markers.length == data.length) doFilter();

	});

To be honest, I think this is better overall as there really is no need to keep geocoding the same darn address for every user who comes to your site. You can find a full demo below.

Related Blog Entries

With SDKs for all major client and server side platforms, you can experiment on any platform with Optimizely Full Stack.

Topics:

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}