Sunday, May 08, 2011

Zoom tooltip hack

Average users don't care about zoom level numbers, but some people do. Developers do.

I created a 'semihidden' zoom level display for my pages. Google provides hints like "zoom in" and "zoom out" if you hover the zoom control element (slider). I thought that average map users already know those things.

Try hovering zoom slider of the first quadtree example page. Yes, hovering the zoom slider will display also meters/px resolution. That is an extra bonus for those who are interested in the World.

Have a look in the source code. It is commented there as "zoom tooltip hack". Feel free to copy the hack, if you like it. You can molest the code any way you like.  Shall this post be the license.

Thursday, May 05, 2011

Distance Matrix

Distance Matrix service was introduced in the latest update of v3 API. It returns maximum 100 driving distances and durations by a single request. Reference.

All of you who have a store locator, can now add true driving distances and estimated driving times in search results. Also you can filter out results that exceed a distance limit. If user searches by 100 km radius, you may want to return only results that have a true driving distance less than 100 km.

It is like driving directions without directions. Some people have tried to implement something similar by making multiple driving distance requests. Many of them faced problems with the asynchronous responses not coming back in the same order that requests were made. The new service is very simple to use.

It has travelMode option too. So it can be used for walking distances in city maps.

This is a useful service.

Thursday, April 28, 2011

Quadtree

Quadtree is a powerful spatial indexing technique. Wikipedia.

Every now and then I been thinking to implement a collision detect function using quadtree. Playing with quadtrees is so fascinating that I haven't got as far as collision detect, but I have a simple general quadtree demo page online. Play with it. It's inspiring.

If you are interested in experimenting, I enclosed the basic functions in QT.js library. The collision detector will be there too but I have some different applications in mind first.

Monday, March 14, 2011

Category sidebar - there is no sense without priority

Thanks for many of you that been applying the category sidebar script I wrote a long time ago for v2. Sure It works on desktop but what happens if all the entries don't fit in the limited viewport of a mobile browser?

Web has changed since. Now we have to make pages that can be used by mobile browsers too. The screen size can be equal or smaller to the first Mac.

The idea of 'gategory' is still living strong. I have extended makeMarker so that each marker has a 'cat' property. If you click one category visible, all those markers and their sidebar entries do not simply fit in view in iPhone. Also remember that mobile 'touch' devices do not support scrollbars at all. They simply cut the material! (People are shouting about Flash, hehe!)

We have to bring one more parameter to a marker - that is priority. In my current design, the default priority is the distance from map center.

The smallest mobile screen can display a sidebar with a few (<10) entries. You have to decide, which of those 1000 they are.

Ideas?

Friday, December 10, 2010

v3 Marker z-index, the way I thought it was

The way it is

Marker.getZindex() returns 'undefined' if you did not set any value for it. Looking under the hood reveals that actually there is a latitude dependent value set for the marker DOM element. The value seems to be equal to 'top' position of the marker in map container. That is clever. It takes care that markers are displayed in right order by latitude (South on front). But why is the value not returned?

Set any value for Marker z-index. Now getZIndex() returns the value and that set for the Marker DOM element. So setting a z-index value of your own ruins the order set by API.

The way it might be

User z-index and actual DOM z-index should be separated.

Markers should have the default DOM z-index as it is now.

Marker.getZindex() should return zero if you did not set any z-index.

Setting a z-index value would be added to the default value.

So user z-index would be relative to the actual DOM value.

API would take care of order by latitude (of overlays with equal z-index) like it currently does without any z-index settings.

Why

That would make the zIndex property of Marker, Circle, InfoWindow etc much easier to use. Currently you have to rewrite all the z-index values if you want to raise z-index of a single marker..

I have a feeling that it was originally meant to work that way.

Saturday, October 16, 2010

Automatic API v3 reference

I have a test page that analyzes contents of google.maps namespace. I've had it for a long time. The trouble was that controlling the version needed some hackery with v3. Now when the version control was released, I decided to polish and share the page. Actually it is a site of three pages, one for each version. Frozen (3.0), Release (3.1) and Nightly (3.2).

API v2 is from 2005 and no namespace thinking was applied. The constructors and all the stuff that v2 offers is introduced in global scope. I never faced any troubles with that but nowadays some people like it as 'bad programming practice'. Actually it really was much trickier to write a similar test page for v2. Global scope (window) has a lot of stuff that is introduced by the browser and must be filtered.

The test page scans google.maps object by a for-in loop. No rubbish is found and no additional filtering is needed. The results contain the constructors and some objects. Those are listed on page and also on the index in the header. All the members that were found are scanned by a for-in loop again. This time looking inside prototype objects. If prototype is not found, the object itself is looped. Resulting properties are listed in a row.

The pages are coded for Firefox only. Firefox presents some unique features for this kind of work. One of them is .toSource() method of Object. It creates a string representation of source code. Firefox presents source code in nice prettyprinted format in alert() dialog box.

With Firefox, you can click any listed method on page and see its internal code. Also there are links to search the method name from documentation, API forum or Google Code Search.

The resulted listing proves that API v3 is written following good programming practices. Almost no internal variables are leaking. The internal source code of the constructors and their methods doesn't reveal much. Still it is worth exploring, you always find something that deepens your understanding about API.

The documentation practice of v3 is different to v2 in the way that there are not many undocumented methods in v3. Still you can find some. Today I saw some new methods of Marker.

You can use the pages for comparing versions. Yes, they are documented separately but still you can find something meaningful.

Tuesday, April 27, 2010

Circle in v3

Circle object of v3 creates a circular overlay as expected. It is more useful than I first thought.

If you want to zoom your map to show an area of say 50 km radius, that would mean some maths. Create a Circle() of 50 km radius and take its bounds by now provided getBounds(). You can use the bounds to zoom the map by fitBounds(). No calculations needed. An example.

Circle can be used as a marker as well. It is clickable and it works as an anchor for InfoWindow. Marker keeps its size when you zoom. Circle does not but Circle has a minimum size based on stroke width. That is actually nice behavior in many cases. Now there is an updated version of makeMarker(). It creates a Circle instead of Marker if you set any value to 'radius' option.

'title' option does not create a flyover tooltip. However Circle supports all the usual mouse events. So you can create a custom tooltip for the purpose.

Sunday, February 21, 2010

Custom icons

Just discovered a custom icon collection project on Google Code. There are more than 900 icons this far.

It would be nice if those were provided in sprite format. Happily there are sprite-making services. SpriteMe is the most advanced of those.

.

Monday, February 08, 2010

clearOverlays() in V3

There is no clearOverlays() in API v3. Some practices have been presented. I think this is the simpliest so far.

Push all the overlays in an array when created (as usual). Following code will clear both map and the array:

  while(overlays[0]){
   overlays.pop().setMap(null);
  }

pop() method of an array removes the last element of an array, and returns that element. 'while' keeps that happening as long as there are elements in the array. When overlays[0] does not exist anymore, the mission is completed and code will proceed.

Applied in the example of the previous post.

Rectangle() and Circle() of V3

API v3.28 has new constructors

  new google.maps.Rectangle();
  new google.maps.Circle();

Dimensioning and positioning is set by bounds (Rectangle) or by center and radius (Circle). Circle radius is given in meters. Those can be set as options or by special methods setBounds(), setCenter() and setRadius().  They are documented already.

getBounds() for Circle would be a useful method that is missing.

Altogether those are very handy classes and we can forget my boundsbox.js library in most cases. Image overlaying is still a perfect task for boundsbox.

Quickly testing Rectangle().

.

Friday, January 22, 2010

Suggesting geocoder [3]

From the previous post: "Next logical step of development will be to display all the alternative addresses as a selectable list."

While developing, I found this kind of user interface very handy. You can even learn something new while typing. I like it better than classic maps.google.com.

Maybe I should give a hint to maps.google.com people:)

Play with it and comment please.


<joke>
Many geocoders recognize a placename "undefined".

Thursday, January 21, 2010

Buttonless geocoder [2]

Google's geocoding service is enhanced continuously. It doesn't read your thoughts yet but it is quite forgiving with typos and missing characters.

Now you can see the geocoding result after every character you type in the new geocoder experiment. When you agree with the result in the display, click outside text field or hit Tab or Enter (not IE) and the map is updated too.

So the geocoding request is sent by 'keyup' and map is set by 'change' events of text input field. Clicking on map does not blur the text field and does not trigger 'change'. That is still waiting for a great idea.

Some people can type damn fast. Therefore the geocoding requests are limited to maximum three requests per second.

The 'hint' display shows the first address of the response set. Next logical step of development will be to display all the alternative addresses as a selectable list.

Sunday, January 17, 2010

Custom map types in v3

The latest version update (3.25) introduced custom map types for api v3. The interface is nice. If you are familiar with custom tile sets in v2, there are not many new things to learn. There are three types of custom tile based map types.
  • Image map type, like Open Streetmap tiles in the example page
  • Image overlay map type, like the Traffic tiles in the example page
  • Non-image tile based map type
Image Map Type behaves just like the standard map types. Image Overlay tiles are displayed on top of the Image Map Types. Both of them can be created using ImageMapType class. The difference is how you add the map type to your map. Either to mapTypes registry or to overlayMapTypes MVCArray. Both of them are properties of Map.

The non-image map type makes it possible to create tile layers with SVG, canvas or any html elements.

Tuesday, December 08, 2009

Geocoder without Search button

We are used to see geocoders with address input field and [Search] button.

We can discard the button and use 'onchange' event of the input field instead.

It works quite well. Just type in the address and click anywhere on the page. Well not anywhere because click on map is preventDefaulted. Hitting [Enter] key works too. @M$: please don't file a patent application.

Text input field can also be used for showing how the geocoder actually understood the address.

Full example.

Monday, December 07, 2009

Sidebar with makeMarker() v3

Wouldn't that be convenient if makeMarker(options) would construct a sidebar entry as well. See it here.

A separate SidebarItem() constructor was written. It creates a button element and appends that to a div with id="sidebar" by default. makeMarker() calls the constructor if you have set an option property {sidebarItem: "My label"}

The basic button element needs some CSS cure. You can also style individual sidebar items by setting different CSS class names for them. {sidebarItemClassName: "important_item"}

The events are set to work bidirectionally.
  • click on sidebar -> click on marker
  • select on sidebar -> click on marker (select by [Tab])
  • click on marker -> focus on sidebar
  • mouseover on sidebar -> mouseover on marker
  • mouseout on sidebar -> mouseout on marker
One of the reasons why I selected a button element was that it is 'focusable' and 'selectable'. You can open an info window by stepping the focus to a sidebar item by [Tab] or [Shift]+[Tab] and select the item by [Enter] or [Space] (some browsers). That is called accessibility.

Also you can use :hover and :focus CSS pseudoselectors. (:focus does not work with IE)

SidebarItem() instance has methods addIn() and remove(). We need addIn() to decide where to add the item. That helps to implement the categories.

Thursday, November 26, 2009

v3 markers and info windows

v3 constructors like Marker(opts) and InfoWindow(opts) take an options object as their single parameter.

How about composing a createMarker(opts) function that also takes a single options object and creates both Marker and Infowindow.

Marker and InfoWindow options have 'position' and 'zIndex' properties in common. All the other property names are unique.

You have to give 'position' for a Marker. Giving the same position to InfoWindow has no effect if InfoWindow.open() is given an anchor.

There is no need to set zIndex of InfoWindow if you are using just a single instance of InfoWindow. It even doesn't have any visible effect. So we can freely use zIndex to control the marker.

There was no need to convert any property names. makeMarker(opts) function can be seen in action here.

The single options object can further be extended to include new properties like 'category' and 'sidebarLabel'.

Wednesday, October 14, 2009

Custom tooltip for v3

v3 BoundsBox library provides constructors to create a rectangular overlay by LatLngBounds or by LatLng plus km-dimension (also miles or nautic miles). You can also overlay images that scale with zoom.

It is still missing an overlay with pixel dimensions. For tooltips and other labeling. It is coming.

I haven't included the 'tiptool' to BoundsBox library yet. There are a few things still under development.

I am impressed with 'universional' concept of a library that Paul Kulchenko has implemented. He has written some impressive Canvas experiments that work with v2 and v3 with one and only script.

That is the way that Google Maps API libraries should be written.

Thursday, September 24, 2009

Gecoder response structure

I made simple test pages for version 2 and version 3 that display the geocoder response object contents prettyprinted.

First I was using toSource() method of Firefox which is a handy way to have a look inside objects. You could say that is opposite to eval().

Using non-standard toSource() debugging method means that the pages would work only with FF. I think that all the browsers have a similar method internally, but it is exposed only in Gecko browsers.

I switched to JSON.stringify(). That is also built in FF3.1+ and even in IE but only in IE8 mode. The function is made cross-browser by loading Douglas Crockfords json2.js script. You can get it from json.org.

The line breaks, spaces and indents are generated by beautify.js from jsbeautifier.org

Monday, September 14, 2009

Zoom events

The following concerns version 2 of API.

There is a single zoomend(old, new) event in documentation. However there are a few additional undocumented events that return a lot of data about zoom action.

'zoomto' is triggered always when user changes zoom setting. It returns two parameters. The first one is a string that indicates how the zoom was started. The second is an object {infoWindow: boolean} that tells if the info window is open. Following string values are discovered:

- "zi" or "zo" When zoom button is clicked
- "zs_drag_zi " or "zs_drag_zo" When zoom slider is moved
- "zb_click_zi" or "zb_click_zo" When zoom bar is clicked
- "wl_zi" or "wl_zo" By scrollwheel zoom
- "key_zi" or "key_zo" When zoomed by keyboard +/-

If we have continuousZoom enabled, we get more events: 'zoomstart' and 'zooming'.

'zooming' is triggered repeatedly while animated zoom is under progress. The interval is a few tens of milliseconds.

'zoomstart' is triggered when continuous zoom is started by dblclick or scrollwheel. Its first parameter is -1 or 1 indicating the direction of the started zoom. The second parameter is a GLatLng mouse pointer position. By double rightclick zoomout the second parameter is a blank object.

I have discovered a third parameter, a boolean that is true only by dblclick-zoom-in?

When triggering a continuous zoom, we get a series of events:
  • zoomstart
  • zoomto
  • zooming
  • zooming
  • zooming
  • zoomend
And a lot of data with them.

Friday, August 28, 2009

Accessibility and Google Maps

My understanding about accessibility of a web page is, that all its contents must be accessible without a mouse.

So was the Map Kitchen examples written. Take categories example and you can open any info window by [Tab] key, check the checkboxes by [space]. ([Shift[Tab]] to go back).

Accessibility is in the hands of the developer who writes the code for a map page. It is not built in Google Maps.

Patrick H. Lauke from Opera does not accept my opinion. We had a short public discussion on the forum and our opinions are:

Patrick:
Google Maps control elements (zoom, nav-buttons...) should be keyboard accessible

My opinion:
Those functions are easily made keyboard accessible by the page developer but they are not important. The contents must be accessible. There should be a method to open any info window without mouse.

Patric has made great work with a script that converts Google Maps control buttons keyboard accessible