Skip to content

Commit

Permalink
update to version 1.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Onur YILDIRIM committed Jan 9, 2015
1 parent 3bb627b commit 3957f94
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- No library/framework dependencies (such as jQuery, MooTools, etc...)
- Browser Support: IE 9+, Chrome, Safari, Firefox, Opera...

**Download:** [Full Version](https://raw.github.com/onury/geolocator/master/src/geolocator.js) 12.4KB *(3.2KB gzipped)*, [Minified Version](https://raw.github.com/onury/geolocator/master/src/geolocator.min.js) 4KB *(1.6KB gzipped)*
**Download:** [Full Version](https://raw.github.com/onury/geolocator/master/src/geolocator.js) 13KB *(3.5KB gzipped)*, [Minified Version](https://raw.github.com/onury/geolocator/master/src/geolocator.min.js) 4KB *(1.8KB gzipped)*

See a live [**demo here**][demo].

Expand All @@ -36,8 +36,8 @@ Inside the `<head>` of your HTML:
console.log(location);
}
//The callback function executed when the location could not be fetched.
function onGeoError(message) {
console.log(message);
function onGeoError(error) {
console.log(error);
}
window.onload = function() {
Expand All @@ -53,7 +53,7 @@ Also place the line below, inside your `<body>` if you want to dynamically draw
<div id="map-canvas" style="width:600px;height:400px"></div>
```

geolocator.js provides 2 useful methods:
geolocator.js provides 3 useful methods:

##Methods

Expand All @@ -69,7 +69,7 @@ geolocator.locate( successCallback, [errorCallback], [fallbackToIP], [html5Optio
> A callback function to be executed when the location is successfully fetched. The recent `geolocator.location` object is passed to this callback, as the only argument.
> - `errorCallback` *Function (optional, default: `null`)*
> A callback function to be executed when the location could not be fetched due to an error. The recent error message `String` is passed to this callback, as the only argument.
> A callback function to be executed when the location could not be fetched due to an error. The recent `PositionError` object is passed to this callback, as the only argument.
> - `fallbackToIP` *Boolean|Integer (optional, default: `false`)*
> Specifies whether geolocator should fallback to IP geo-lookup when HTML5 geolocation is not supported, timeout expired, position is unavailable or permission rejected by the user. A positive `Integer` value will indicate the index of the source ip-geo service (if the value is in range). Boolean `true` will set the default ip-geo service index which is `1` (GeoPlugin). Valid values: *`0` or `true` (use FreeGeoIP for ip-geo fallback), `1` (use GeoPlugin for ip-geo fallback), `2` (use Wikimedia for ip-geo fallback), `false` or `-1` or `null` or any other value (will disable ip-geo fallback)*
Expand Down Expand Up @@ -101,7 +101,7 @@ geolocator.locateByIP( successCallback, [errorCallback], [ipSourceIndex], [mapCa
> - `errorCallback` *Function (optional, default: `null`)*
> A callback function to be executed when the location could not be fetched due to an error.
> The recent error message `String` is passed to this callback, as the only argument.
> The recent `Error` object is passed to this callback, as the only argument.
> - `ipGeoSourceIndex` *Integer (optional, default: `0`)*
> Indicates the index of the IP geo-lookup service.
Expand All @@ -115,6 +115,17 @@ geolocator.locateByIP( successCallback, [errorCallback], [ipSourceIndex], [mapCa
geolocator.locateByIP(onGeoSuccess, onGeoError, 1, 'map-canvas');
```

###`geolocator.isPositionError()`
Checks whether the type of the given object is HTML5 `PositionError` and returns a `Boolean` value.
```js
geolocator.isPositionError( error )
```

**Parameters:**

> - `error` *Object*
> Object to be checked.
##Properties

###`geolocator.location` *Object*
Expand Down Expand Up @@ -158,6 +169,11 @@ Provides the recent geo-location information.

###Change Log:

**version 1.2.6**
- Revision: The recent `Error` or `PositionError` (HTML5) is passed to error callbacks instead of `String` error message. See updated documentation. Fixes issue [#7](https://github.com/onury/geolocator/issues/7). *(This shouldn't be a breaking-change but do test your app if you decide to upgrade.)*
- Feature: Added new method: `isPositionError()`.
- Updated example. (Added checkbox for HTML5-to-IP fallback.)

**version 1.2.4**
- Revision: Source scripts are now automatically removed from DOM after result is received.
- Bug-Fix: `errorCallback` would not be invoked if IP geo-source service is not available. Fixes issue [#6](https://github.com/onury/geolocator/issues/6).
Expand Down
18 changes: 12 additions & 6 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
document.getElementById('geo-details').innerHTML = strLocation;
}
// Error Callback
function onGeoError(message) {
console.log(message);
function onGeoError(error) {
console.log(error);
// To check if this is a HTML5 `PositionError`:
// console.log(geolocator.isPositionError(error));
}
// Load Event Handler
window.onload = function () {
Expand All @@ -31,14 +33,17 @@
// Configure controls
var cmbSource = document.getElementById('cmb-source'),
btnLocateByIP = document.getElementById('btn-locate-ip'),
btnLocateHTML5 = document.getElementById('btn-locate');
btnLocateHTML5 = document.getElementById('btn-locate'),
chkFallback = document.getElementById('chk-fallback');
// Click Event Handlers
btnLocateByIP.onclick = function() {
geolocator.locateByIP(onGeoSuccess, onGeoError, cmbSource.selectedIndex, 'map-canvas');
}
btnLocateHTML5.onclick = function() {
var html5Options = { enableHighAccuracy: true, timeout: 6000, maximumAge: 0 };
geolocator.locate(onGeoSuccess, onGeoError, cmbSource.selectedIndex, html5Options, 'map-canvas');
var html5Options = { enableHighAccuracy: true, timeout: 6000, maximumAge: 0 },
ipFallbackIndex = chkFallback.checked ? cmbSource.selectedIndex : -1;
geolocator.locate(onGeoSuccess, onGeoError, ipFallbackIndex, html5Options, 'map-canvas');
console.log('IP Fallback is ' + (ipFallbackIndex === -1 ? 'disabled' : 'enabled'));
}
}
</script>
Expand All @@ -55,13 +60,14 @@
<option selected value="1">Geo Plugin</option>
<option value="2">Wikimedia</option>
</select>&nbsp;<button id="btn-locate-ip">Locate by IP</button> | <button id="btn-locate">Locate (HTML5)</button>
<input id="chk-fallback" type="checkbox" checked /> Fallback to IP
<br /><span class="info">(Geo IP Source is also used as a fallback when user rejects HTML5 Geo-location or timeout expires.)</span><br />
</div>
<div id="map-canvas"></div>
<div id="geo-details"></div>
<br />
View <a href="https://github.com/onury/geolocator">project page</a> (and documentation) at GitHub.<br />
Geolocator 1.2.4 - Copyright © Onur Yildirim, 2014. MIT License.
Geolocator 1.2.6 - Copyright © Onur Yildirim, 2014. MIT License.
<br /><br />
</div>

Expand Down
24 changes: 16 additions & 8 deletions src/geolocator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*jslint browser:true, nomen:true */
/*jslint browser:true, nomen:true, regexp:true, unparam:true */
/*global google:false */


/** @license Geolocator Javascript Lib v.1.2.4
/** @license Geolocator Javascript Lib v.1.2.6
* (c) 2014-2015 Onur Yildirim ([email protected])
* https://github.com/onury/geolocator
* MIT License
Expand Down Expand Up @@ -70,7 +70,8 @@ var geolocator = (function () {
}

script.onerror = function (e) {
execCb(onError, 'Could not load source at ' + String(url).replace(/\?.*$/, ''));
var errMsg = 'Could not load source at ' + String(url).replace(/\?.*$/, '');
execCb(onError, new Error(errMsg));
};

script.src = url;
Expand Down Expand Up @@ -188,12 +189,12 @@ var geolocator = (function () {
function getPosition(fallbackToIP, html5Options) {
geolocator.location = null;

function fallback(errMsg) {
function fallback(error) {
var ipsIndex = fallbackToIP === true ? 0 : (typeof fallbackToIP === 'number' ? fallbackToIP : -1);
if (ipsIndex >= 0) {
geolocator.locateByIP(onSuccess, onError, ipsIndex, mCanvasId);
} else {
if (onError) { onError(errMsg); }
if (onError) { onError(error); }
}
}

Expand All @@ -207,13 +208,13 @@ var geolocator = (function () {
}

function geoError(error) {
fallback(error.message);
fallback(error);
}

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, html5Options);
} else { // not supported
fallback('geolocation is not supported.');
fallback(new Error('geolocation is not supported.'));
}
}

Expand Down Expand Up @@ -300,7 +301,7 @@ var geolocator = (function () {
if (initialized === true) {
finalize(geolocator.location.coords);
} else {
if (onError) { onError(data || 'Could not get location.'); }
if (onError) { onError(new Error(data || 'Could not get location.')); }
}
}

Expand Down Expand Up @@ -352,6 +353,13 @@ var geolocator = (function () {
mCanvasId = mapCanvasId;
geolocator.__ipscb = onGeoSourceCallback;
loadIpGeoSource(ipGeoSources[sourceIndex]);
},

/** Checks whether the type of the given object is HTML5
* `PositionError` and returns a `Boolean` value.
*/
isPositionError: function (error) {
return Object.prototype.toString.call(error) === '[object PositionError]';
}
};
}());
20 changes: 10 additions & 10 deletions src/geolocator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3957f94

Please sign in to comment.