Ext

Integrating Google Maps API With ExtJS

July 1, 2008 by Shea Frederick

StreetViewTheres no doubt that Google has some interesting and very useful JavaScript API’s - most of which I end up using over and over again. So why not package them up into an Ext component? Well thats exactly what I decided to do, adding a simple component centered around the Google Maps API.

  • Maps Panel
  • StreetView Panel

These are components that extend from Panel and showcase just how easy it is to make ExtJS do whatever you need. Since they extend from Panel, it means you can use them anywhere a panel can be used - such as a window, viewport or layout.

In this first release I am only supporting map types of ‘map’ (what Google refers to as the ‘Normal Map’) and the street view ‘panorama’, but I hope to be able to integrate the Moon, Mars and Sky maps soon enough.

With the extended component and a little bit of code we have a functional Google street view window with panning, zoom and navigation.

var panwin = new Ext.Window({
	layout: 'fit',
	closeAction: 'hide',
	title: 'GPanorama Window',
	width:400,
	height:300,
	x: 480,
	y: 60,
        items: {
		xtype: 'gmappanel',
		gmapType: 'panorama',
		setCenter: {
			lat: 42.345573,
			'long': -71.098326
		}
	}
});

GMapIt’s just as easy to create a Google map window that maps addresses and places markers at their locations (which could just as easily be nested in a layout instead).

A couple of the primary Google maps handlers and settings are setup as configuration options. For instance, ‘addControl’ allows adding of a standard Google map control (zoom, pan, etc) and the ‘zoomLevel’ sets a default zoom level for the map.

Geocoding can be used by substituting the lat/long configuration options with a ‘geoCodeAddr’ string.

The ’setCenter’ configuration allows the default center location of the map to be set, along with a marker. More markers can be added to the map using the ‘markers’ array.

var mapwin = new Ext.Window({
        layout: 'fit',
	title: 'GMap Window',
        closeAction: 'hide',
	width:400,
	height:400,
	x: 40,
	y: 60,
        items: {
            xtype: 'gmappanel',
            region: 'center',
    		zoomLevel: 14,
    		gmapType: 'map',
    		addControl: new GSmallMapControl(),
    		setCenter: {
    			geoCodeAddr: '4 Yawkey Way, Boston, MA, 02215-3409, USA',
    			marker: {title: 'Fenway Park'}
    		},
    		markers: [{
    			lat: 42.339641,
    			'long': -71.094224,
    			marker: {title: 'Boston Museum of Fine Arts'}
    		},{
    			lat: 42.339419,
    			'long': -71.09077,
    			marker: {title: 'Northeastern University'}
    		}]
        }
});

GMap All

With a bit more work, we could have the many offerings of the Google Maps API integrated for easy usage with ExtJS. For now the functionality thats missing can be accessed using the ‘getMap’ handler.

Take a look at the code, see how to use it, or just play with the example.

29 Responses to “Integrating Google Maps API With ExtJS”

  1. Rich Waters » Blog Archive » Ext & Google Maps

    [...] out the blog post for full details and code, or jump straight to the live example to play with it (don’t forget [...]

  2. serkanyersen

    It looks great. A context menu for marking or opening a panaroma view for specific place would be great.

  3. Luuk Peters

    Can’t wait to try it out, looks very nice.
    I’m also going to try out Ext GWT, will this also be available in Ext GWT?

  4. Google Map Component for ExtJS | My Other Beans : Claude Betancourt's Blog

    [...] Shea Frederick has created a user-defined extension to integrate Google Maps with ExtJS. [...]

  5. Murat Çorlu

    Perfect job! This is very helpfull.

  6. Daniel Haas

    Very nice! I would really like to use this, but unfortunatly GoogleMaps does not let you use the maps api for commercial applications. :-(
    Has anyone coded something similar, but using the Yahoo Maps API?

  7. Shea Frederick

    @Daniel - Google does offer a commercial usage option - http://www.google.com/enterprise/maps/

    The Yahoo JavaSript maps API is quite similar to the Google Maps API, so I would imagine you could start with this code and not need to do a whole lot to switch it over.

  8. Bo

    Very nice.

  9. mdmadph

    @Shea

    I shudder to think what they’re charging, though. -_-’

  10. raphael

    Error in my localhost:-> this.B is null

  11. Shea Frederick

    @raphael - I have noticed that happening when there is a slow response getting the map from Google and adding markers happens before the map is rendered. I was not able to find an appropriate event to attach adding the markers to, so I added a delay (line 67) which eliminated this problem for me, but it appears this can still cause problems for some users.

    Increasing the delay time will help, or finding the appropriate event.

  12. Lars Kneschke

    What’s the license of the published code?

  13. Shea Frederick

    @Lars - Free as a bird. Use it anywhere or any way you like.

  14. SilverJo

    Very nice Frederick. I was playing with this too and created also some kind of extension for Google maps and Live Earth maps 2 weeks ago.
    When looking at your code I already find answers for things I was struggling with. Thanks for the share!

  15. Putting Google Maps on your site with ExtJs | foojam.com

    [...] Frederick has a post on the ExtJs blog on how to integrate Google Maps with your ExtJs application. Very cool stuff. I can’t wait to see mapping play a bigger and bigger part on websites. You [...]

  16. raphael

    Very good!
    Increasing the delay time solved the problem.
    Thanks!

  17. E Bulka

    This is great stuff. I have a project which required this and I was just about to do it the manual way so you saved me a lot of time.

    Just a suggestion: You seem to need quotes around long because it is a reserved word and might otherwise be misinterpreted. Why, then don’t you use Google’s standard - lng - or the more recognized standard - lon?

  18. Daily del.icio.us for July 1st through July 4th — Vinny Carpenter’s blog

    [...] Ext JS - Integrating Google Maps API With ExtJS - Theres no doubt that Google has some interesting and very useful JavaScript API’s - most of which I end up using over and over again. So why not package them up into an Ext component? Tags: agile, ajax, api, appserver, AquaLogic, architecture, batch, BEA, business, development, esb, extjs, gears, google, googlemaps, gwt, hacks, http, infoq, J2EE, java, java7, javascript, methodology, microsoft, netflix, news, nio, opensource, oracle, politics, presentation, programming, projectmanagement, rant, roku, screencast, scrum, security, SOA, software, spring, SpringFramework, streaming, sync, tools, usa, utilities, video, WebLogic, windows, xss [...]

  19. willgillen

    I added some “onclick” capabilites to the map markers.
    Is there somewhere (forum thread, wiki page, other) where I can post this update?

  20. Shea Frederick

    @willgillen - added a forum post, and would love to see your additions posted there.
    http://extjs.com/forum/showthread.php?p=191301

    @E Bulka - thanks for pointing that out, I cant think of a good reason to not use ‘lng’ instead of ‘long’ - the thought just never crossed my mind. I have adjusted this in the forum post code to use ‘lng’ since thats what Google Maps uses.

  21. Riz

    Simply brilliant!

  22. Веб-обзор #14: в помощь игроразработчику, оптимизация и кеширование в Zend Framework, немножко web 2.0 экономики, ExtJS и MySQL, а также фичкаттинг в стартап

    [...] Integrating Google Maps API With ExtJS - если вы используете в разработке такой мощнейший фреймворк для создания веб-интерфейсов, как ExtJS, и сталкиваетесь с потребностью работать с картами от сервиса Google Maps, то здесь вы найдете красивые и функциональные примеры интеграции карт в ваши приложения на ExtJS. На мой скромный взгляд это, вместе  со средой Adobe AIR и поддержкой AIR-а в ExtJS может действительно сделать небольшую революцию в хорошем смысле, на рынке веб-приложений. Попробуйте, это красиво, практично и просто! [...]

  23. Max

    how should I be using this. It looks really nice. Can someone help me with this

  24. dmat

    Hi willgillen , Could you please post the code for the “onclick” capabilites added to the map markers?

    Thanks,
    dmat

  25. Werbung Hamburg

    Thanks for the helpful information

  26. Integrating Google Maps API With ExtJS : No Matter What

    [...] http://extjs.com/blog/2008/07/01/integrating-google-maps-api-with-extjs/ [...]

  27. Ext JS - Blog

    [...] This extension was originally written up as a demo for one of our previous blog posts. However, it proved to be such a hit with the community that we transformed it into an official [...]

  28. Andrey Kostenko. Home » Blog Archive » Вышел ExtJS 2.2

    [...] This extension was originally written up as a demo for one of our previous blog posts. However, it proved to be such a hit with the community that we transformed it into an official [...]

  29. heidtmare

    how can i get this to play well with toolbars?
    my map goes under my bbar…

Leave a Reply

To prove that you're not a bot, please answer this question:



© 2006-2008 Ext, LLC