开发者

Interacting with Google's Geolocation Detection

I have two issues with Google's location detection. I am using the basic code of this example: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/examples/map-geolocation.html

1.) When a visitor declines his location to be detected, then the map will not show. That cripples the site. How can I show a fallback map with standard functionality and without location detection?

2.) Is there a way to interact with the result of the browser dialogue that asks the viewer to detect his location? If a viewer declin开发者_JS百科es his location to be detected and if the above point 1.) is not possible, then it would be good to hide the map alltogether.


In the else part of the handleNoGeolocation function you would provide your fallback code giving a default location or another means of location detection.

Here's an alternative structure for the functions that's a little drier and uses Google's IP geolocation for fallback:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(updatePosition, ipPosition, { options... });
} else {
  ipPosition();
}

function ipPosition() {
  updatePosition(google.loader.ClientLocation);
}

function updatePosition(position) {
  if (position == null) {
    return updatePosition(defaultPlace());
  }

  // Firefox 3.6 passes in a non-standard position object with lat/lng at the top level instead of in .coords
  var coords = (position.hasOwnProperty("coords")) ? position.coords : position;
  if (coords == null) {
    return updatePosition(defaultPlace());
  }

  [...]
}


In that sample there is a function handleNoGeolocation that they use when the user don't have or declines positioning. With the same technique you should be able to handle the hiding of the map.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜