Commit 796d7ba2 authored by Mohsen Taleb's avatar Mohsen Taleb

Updated SDK to comply with mapbox v2.2.3. Improved README and demos.

parent d773d470
......@@ -10,14 +10,14 @@ interactivity.
| Options | Value | Description |
| ---- | ---- | ---- |
| element (_required_) | string | Must be the id of an element, or a DOM element reference. |
| id _or_ url _or_ tilejson | __string__ if _id_ or _url_ __object__ if _tilejson_ | url can be <ul><li>A map `id` string `examples.map-foo`</li><li>A comma separated list of map `id` strings `examples.map-foo,examples.map-bar` [example](https://www.mapbox.com/mapbox.js/example/v1.0.0/compositing/)</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/examples.map-0l53fhk2.json`</li><li>A [TileJSON](https://www.mapbox.com/developers/tilejson/) object, from your own Javascript code</li></ul> |
| id _or_ url _or_ tilejson | __string__ if _id_ or _url_ __object__ if _tilejson_ | url can be <ul><li>A map `id` string `examples.map-foo`</li><li>A comma separated list of map `id` strings `examples.map-foo,examples.map-bar` [example](https://www.mapbox.com/mapbox.js/example/v1.0.0/compositing/)</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/mapbox.dark.json`</li><li>A [TileJSON](https://www.mapbox.com/developers/tilejson/) object, from your own Javascript code</li></ul> |
| options | object | If provided, it is the same options as provided to L.Map with the following additions: <ul><li>`tileLayer` L.TileLayer options. Options passed to a `L.cedarmaps.tileLayer` based on the TileJSON. Set to `false` to disable the `L.cedarmaps.tileLayer`.</li><li>`featureLayer` `L.cedarmaps.featureLayer` options. Options passed to a `L.cedarmaps.featureLayer` based on the TileJSON. Set to `false` to disable the `L.cedarmaps.featureLayer`.</li><li>`gridLayer` `L.cedarmaps.gridLayer`. Options passed to a `L.cedarmaps.gridLayer` based on the TileJSON. Set to `false` to disable the `L.cedarmaps.gridLayer`.</li><li>`legendControl` `L.cedarmaps.legendControl` options. Options passed to a `L.cedarmaps.legendControl` based on the TileJSON. Set to `false` to disable the `L.cedarmaps.legendControl`.</li><li>`shareControl`: Options passed to a `L.cedarmaps.shareControl`. Set to `true` to enable the `L.cedarmaps.shareControl`.</li><li>`infoControl`: Options passed to a `L.cedarmaps.infoControl`. Set to `true` to enable the `L.cedarmaps.infoControl`.</li><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this map.</li><li>`attributionControl`: value can be `{compact: true}` to force a compact attribution icon that shows the full attribution on click, or `{compact: false}` to force the full attribution control. The default is a responsive attribution that collapses when the map is less than 640 pixels wide.</li> |
_Example_:
// map refers to a <div> element with the ID map
// examples.map-4l7djmvo is the ID of a map on Mapbox.com
var map = L.cedarmaps.map('map', 'examples.map-4l7djmvo');
// mapbox.streets is the ID of a map on Mapbox.com
var map = L.cedarmaps.map('map', 'mapbox.streets');
// map refers to a <div> element with the ID map
// This map will have no layers initially
......@@ -45,16 +45,16 @@ interface to layers from Mapbox and elsewhere.
| Options | Value | Description |
| ---- | ---- | ---- |
| id _or_ url _or_ tilejson (_required_) | __string__ if _id_ or _url_ __object__ if _tilejson_ | Value must be <ul><li>An `id` string `examples.map-foo`</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/examples.map-0l53fhk2.json`</li><li>A TileJSON object, from your own Javascript code</li></ul> |
| id _or_ url _or_ tilejson (_required_) | __string__ if _id_ or _url_ __object__ if _tilejson_ | Value must be <ul><li>An `id` string `examples.map-foo`</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/mapbox.dark.json`</li><li>A TileJSON object, from your own Javascript code</li></ul> |
| options | object | The second argument is optional. If provided, it is the same options as provided to `L.TileLayer`, with the following addition: <ul><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this layer.</li></ul> |
_Example_:
// the second argument is optional
var layer = L.cedarmaps.tileLayer('examples.map-20v6611k');
var layer = L.cedarmaps.tileLayer('mapbox.streets');
// you can also provide a full url to a TileJSON resource
var layer = L.cedarmaps.tileLayer('{{site.tileApi}}/v3/examples.map-0l53fhk2.json');
var layer = L.cedarmaps.tileLayer('{{site.tileApi}}/v3/mapbox.dark.json');
_Returns_ a `L.cedarmaps.tileLayer` object.
......@@ -67,7 +67,7 @@ zoom bounds and other metadata.
_Example_:
var layer = L.cedarmaps.tileLayer('examples.map-20v6611k')
var layer = L.cedarmaps.tileLayer('mapbox.streets')
// since layers load asynchronously through AJAX, use the
// `.on` function to listen for them to be loaded before
// calling `getTileJSON()`
......@@ -91,7 +91,7 @@ _Example_:
// Downsample tiles for faster loading times on slow
// internet connections
var layer = L.cedarmaps.tileLayer('examples.map-20v6611k', {
var layer = L.cedarmaps.tileLayer('mapbox.streets', {
format: 'jpg70'
});
......@@ -106,13 +106,13 @@ interactivity into your map, which you can easily access with `L.cedarmaps.gridC
| Options | Value | Description |
| ---- | ---- | ---- |
| id _or_ url _or_ tilejson (_required_) | __string__ if _id_ or _url_ __object__ if _tilejson_ | <ul><li>An `id` string `examples.map-foo`</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/examples.map-0l53fhk2.json`</li><li>A TileJSON object, from your own Javascript code</li></ul> |
| id _or_ url _or_ tilejson (_required_) | __string__ if _id_ or _url_ __object__ if _tilejson_ | <ul><li>An `id` string `examples.map-foo`</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/mapbox.dark.json`</li><li>A TileJSON object, from your own Javascript code</li></ul> |
| options | Object | The second argument is optional. If provided, it may include: <ul><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this layer.</li></ul> |
_Example_:
// the second argument is optional
var layer = L.cedarmaps.gridLayer('examples.map-20v6611k');
var layer = L.cedarmaps.gridLayer('mapbox.light');
_Returns_ a `L.cedarmaps.gridLayer` object.
......@@ -155,7 +155,7 @@ zoom bounds and other metadata.
_Example_:
var layer = L.cedarmaps.gridLayer('examples.map-20v6611k')
var layer = L.cedarmaps.gridLayer('mapbox.light')
// since layers load asynchronously through AJAX, use the
// `.on` function to listen for them to be loaded before
// calling `getTileJSON()`
......@@ -187,7 +187,7 @@ from Mapbox and elsewhere into your map.
| Options | Value | Description |
| ---- | ---- | ---- |
| id _or_ url _or_ geojson | __string__ if _id_ or _url_ __object__ if _tilejson_ | Must be either <ul><li>An id string examples.map-foo</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/examples.map-0l53fhk2.json`</li><li>A GeoJSON object, from your own Javascript code</li><li>`null`, if you wish to only provide `options` and not initial data.</li></ul> |
| id _or_ url _or_ geojson | __string__ if _id_ or _url_ __object__ if _tilejson_ | Must be either <ul><li>An id string mapbox.streets</li><li>A URL to TileJSON, like `{{site.tileApi}}/v3/mapbox.dark.json`</li><li>A GeoJSON object, from your own Javascript code</li><li>`null`, if you wish to only provide `options` and not initial data.</li></ul> |
| options | object | If provided, it is the same options as provided to `L.FeatureGroup`, as well as: <ul><li>`filter`: A function that accepts a feature object and returns `true` or `false` to indicate whether it should be displayed on the map. This can be changed later using `setFilter`.</li><li>`sanitizer`: A function that accepts a string containing tooltip data, and returns a sanitized result for HTML display. The default will remove dangerous script content, and is recommended.</li><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this layer.</li><li>`popupOptions`: an object of <a href="http://leafletjs.com/reference.html#popup-maxwidth">options that will be passed to the `bindPopup` method internally</a>.<li>`pointToLayer`: A function that accepts a feature object and a `L.LatLng` and returns a layer to be added. Defaults to `L.cedarmaps.marker.style`.</li></ul> |
_Example_:
......@@ -229,9 +229,9 @@ _Example_:
var featureLayer = L.cedarmaps.featureLayer()
.addTo(map);
// loads markers from the map `examples.map-0l53fhk2` on Mapbox,
// loads markers from the map `mapbox.dark` on Mapbox,
// if that map has markers
featureLayer.loadID('examples.map-0l53fhk2');
featureLayer.loadID('mapbox.dark');
_Returns_: the layer object
......@@ -315,18 +315,19 @@ A low-level interface to geocoding, useful for more complex uses and reverse-geo
| Options | Value | Description |
| ---- | ---- | ---- |
| id _or_ url | string | Value must be <ul><li>A [geocoder index ID](https://www.mapbox.com/developers/api/geocoding/), e.g. `mapbox.places`</li><li>A geocoder API URL, like `{{site.tileApi}}/v4/geocode/mapbox.places/{query}.json`</li></ul> |
| id _or_ url | string | Value must be <ul><li>A [geocoder index ID](https://www.mapbox.com/developers/api/geocoding/), e.g. `mapbox.places`</li><li>A geocoder API URL, like `{{site.tileApi}}/geocoding/v5/mapbox.places/{query}.json`</li></ul> |
| options | Object | The second argument is optional. If provided, it may include: <ul><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this geocoder.</li></ul> |
_Returns_ a `L.cedarmaps.geocoder` object.
### geocoder.query(queryString, callback)
### geocoder.query(queryString|options, callback)
Queries the geocoder with a query string, and returns its result, if any.
| Options | Value | Description |
| ---- | ---- | ---- |
| queryString (_required_) | string | a query, expressed as a string, like 'Arkansas' |
| options | object | an object containing the query and options parameters like `{ query: 'Austin', proximity: L.latlng(lat, lng) }`
| callback (_required_) | function | a callback |
The callback is called with arguments
......@@ -439,8 +440,8 @@ Interaction is what we call interactive parts of maps that are created with the
_Example_:
var map = L.cedarmaps.map('map').setView([38, -77], 5);
var gridLayer = L.cedarmaps.gridLayer('examples.map-8ced9urs');
map.addLayer(L.cedarmaps.tileLayer('examples.map-8ced9urs'));
var gridLayer = L.cedarmaps.gridLayer('mapbox.light');
map.addLayer(L.cedarmaps.tileLayer('mapbox.outdoors'));
map.addLayer(gridLayer);
map.addControl(L.cedarmaps.gridControl(gridLayer));
......@@ -472,7 +473,7 @@ the [Mapbox Geocoding API](http://mapbox.com/developers/api/geocoding/).
| Options | Value | Description |
| ---- | ---- | ---- |
| id _or_ url (_required_) | string | Either a <ul><li>An [geocoder index ID](https://www.mapbox.com/developers/api/geocoding/), e.g. `mapbox.places`</li><li>A geocoder API URL, like `{{site.tileApi}}/v4/geocode/mapbox.places/{query}.json`</li></ul> |
| id _or_ url (_required_) | string | Either a <ul><li>An [geocoder index ID](https://www.mapbox.com/developers/api/geocoding/), e.g. `mapbox.places`</li><li>A geocoder API URL, like `{{site.tileApi}}/geocoding/v5/mapbox.places/{query}.json`</li></ul> |
| options | object | An options argument with the same options as the `L.Control` class, as well as: <ul><li>`keepOpen`: a boolean for whether the control will stay open always rather than being toggled. Default `false`. See <a href='https://www.mapbox.com/mapbox.js/example/v1.0.0/geocoder-keep-open/'>live example</a>.<li><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this control.</li><li>`autocomplete`: automatically search and show results as you type. Default: `false`.</ul> |
_Example_:
......@@ -533,12 +534,12 @@ Adds a "Share" button to the map, which can be used to share the map to Twitter
| Options | Value | Description |
| ---- | ---- | ---- |
| id _or_ url _optional_ | string | Either a <ul><li><code>id</code> string <code>examples.map-foo</code></li><li>A URL to TileJSON, like <code>{{site.tileApi}}/v3/examples.map-0l53fhk2.json</code> If not supplied, the TileJSON from the map is used.</li></ul> |
| id _or_ url _optional_ | string | Either a <ul><li><code>id</code> string <code>examples.map-foo</code></li><li>A URL to TileJSON, like <code>{{site.tileApi}}/v3/mapbox.dark.json</code> If not supplied, the TileJSON from the map is used.</li></ul> |
| options | object | Options for L.Control</span> Also accepts the following options:<ul><li>`url`: the <code>URL</code> of a page to which the share control will link instead of the URL of the current page or that specified in TileJSON data.</li><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this control.</li></ul> |
_Example_:
var map = L.map('map', 'examples.map-i875kd35')
var map = L.map('map', 'mapbox.streets')
.setView([37, -77], 5)
.addControl(L.cedarmaps.shareControl());
......@@ -685,7 +686,7 @@ necessary, you can use different tokens on the same page by using the `accessTok
option when creating Mapbox.js objects. For example:
```
var map = L.cedarmaps.map('map', 'examples.map-8ced9urs', {
var map = L.cedarmaps.map('map', 'mapbox.outdoors', {
accessToken: '<your access token>'
});
```
......@@ -719,7 +720,7 @@ Instead you must provide a predefined geocoder index ID (or the ID of a custom g
index). For instance, replace
```
L.cedarmaps.geocoderControl('examples.map-i86nkdio').addTo(map);
L.cedarmaps.geocoderControl('mapbox.outdoors').addTo(map);
```
with
......@@ -785,3 +786,14 @@ is available by applying `class="dark"` to the map div.
_Example_:
<div id="map" class="dark"></div>
## Standalone
By default, Mapbox.js includes a bundled version of Leaflet that Mapbox has ensured
is compatible. A standalone version of Mapbox.js is also available which you can use if you would like to supply your own version of
Leaflet. When using this technique, you will use the newest version of Mapbox.css.
<script src='{{site.tileApi}}/mapbox.js/{{site.mapboxjs}}/mapbox.standalone.js'></script>
<link href='{{site.tileApi}}/mapbox.js/{{site.mapboxjs}}/mapbox.css' rel='stylesheet' />
<script src='your version of Leaflet.js'></script>
# cedarmaps.js
This is CedarMaps Javascript API. It's simply a wrapper for [Mapbox Javascript API](https://github.com/mapbox/mapbox.js/), which is itself built as a [Leaflet](http://leafletjs.com/) plugin. You can [read about its launch](http://mapbox.com/blog/mapbox-js-with-leaflet/).
This is CedarMaps Javascript API. It's simply a wrapper for [Mapbox Javascript API](https://github.com/mapbox/mapbox.js/), (Currently `v2.2.3`) which is itself built as a [Leaflet](http://leafletjs.com/) plugin. You can [read about its launch](http://mapbox.com/blog/mapbox-js-with-leaflet/).
# Table of Contents
- [Basic Usage](#usage)
- [API](#api)
- [Forward/Reverse Geocoding](#forwardreverse-geocoding)
- [Geocoding](#lcedarmapsgeocoderid-options)
- [Geocoding Sample Codes](#geocoding-examples)
- [Reverse Geocoding](#geocoderreversequerylocation-callback)
- [Reverse Geocoding Sample Code](#reverse-geocoding-examples)
- [Building SDK](#building-sdk)
- [Updating SDK](#updating-sdk)
- [Updating mapbox.js submodule](#updating-mapboxjs-submodule)
- [Updating Cedarmaps.js](#updating-cedarmapsjs)
## Usage
Recommended usage is via the CedarMaps CDN:
```html
<script src='http://api.cedarmaps.com/cedarmaps.js/v1.0.1/cedarmaps.js'></script>
<link href='http://api.cedarmaps.com/cedarmaps.js/v1.0.1/cedarmaps.css' rel='stylesheet' />
<script src='http://api.cedarmaps.com/cedarmaps.js/v1.1.0/cedarmaps.js'></script>
<link href='http://api.cedarmaps.com/cedarmaps.js/v1.1.0/cedarmaps.css' rel='stylesheet' />
```
The `cedarmaps.js` file includes the Leaflet library. Alternatively, you can use `cedarmaps.standalone.js`, which does not include Leaflet (you will have to provide it yourself).
......@@ -33,10 +46,90 @@ You can also see the [API documentation](http://mapbox.com/mapbox.js/api/) and [
## API
Managed as Markdown in `API.md`, following the standards in `DOCUMENTING.md`
API is almost the same as mapbox's API. [Check it out](http://mapbox.com/mapbox.js/api/).
However two mostly used methods: forward and reverse geocoding APIs are described below for easy access:
## Building
## Forward/Reverse Geocoding
### L.cedarmaps.geocoder(id, options)
A low-level interface to geocoding, useful for more complex uses and reverse-geocoding.
| Options | Value | Description |
| ---- | ---- | ---- |
| id | string | currently `cedarmaps.streets` |
| options | Object | The second argument is optional. If provided, it may include: <ul><li>`accessToken`: Mapbox API access token. Overrides `L.cedarmaps.accessToken` for this geocoder.</li></ul> |
_Returns_ a `L.cedarmaps.geocoder` object.
example:
```
var geocoder = L.cedarmaps.geocoder('cedarmaps.streets');
```
### geocoder.query(queryString|options, callback)
Queries the geocoder with a query string, and returns its result, if any.
| Options | Value | Description |
| ---- | ---- | ---- |
| queryString (_required_) | string | a query, expressed as a string, like 'Arkansas' |
| options | object | an object containing the query and options parameters like `{ query: 'ونک', limit: 5 }`. <br><br>Other available parameteres: <br><br> <ul><li>`limit` *integer* - Max is 30</li><li>`distance` *float* - Unit is km, 0.1 means 100 meters</li><li>`location` *lat,lon* - For searching near a location. should be accompanied with distance param</li><li>`type` *enum* - Possible values: `locality`, `roundabout`, `street`, `freeway`, `expressway`, `boulevard` <br>(You can mix types by separating them with comma)</li><li>`ne` *lat,lon* - Specifies north east of the bounding box - should be accompanied with `sw` param</li><li>`sw` lat,lon - Specifies south west of the bounding box - should be accompanied with `ne` param</li></ul> |
| callback (_required_) | function | a callback |
The callback is called with arguments
1. An error, if any
2. The result. This is an object with the following members:
{
status: // OK
results: // raw results
}
_Example_: [Live example of geocoder.query centering a map.](http://www.cedarmaps.com/websdk/demos/geocoder-control.html)
_Returns_: the geocoder object. The return value of this function is not useful - you must use a callback to get results.
#### Geocoding Examples:
using a single query parameter:
```
geocoder.query('ونک', function(){});
```
using query string along with an option (Limiting the results):
```
geocoder.query({query:'ونک', limit: 5}, function(){});
```
limiting results based on one or more feature types:
```
geocoder.query({query:'ونک', type: 'locality'}, function(){});
geocoder.query({query:'ونک', type: 'locality,roundabout'}, function(){});
geocoder.query({query:'ونک', type: 'street', limit:2}, function(){});
```
searching within in a specific bounding box:
```
geocoder.query({query:'لادن', ne: '35.76817388431271,51.41721725463867', sw: '35.75316460798604,51.39232635498047'}, function(){});
```
### geocoder.reverseQuery(location, callback)
Queries the geocoder with a location, and returns its result, if any.
| Options | Value | Description |
| ---- | ---- | ---- |
| location (_required_) | object | A query, expressed as an object:<ul><li>`[lon, lat] // an array of lon, lat`</li><li>`{ lat: 0, lon: 0 } // a lon, lat object`</li><li>`{ lat: 0, lng: 0 } // a lng, lat object`</li></ul> The first argument can also be an array of objects in that form to geocode more than one item. |
| callback (_required_) | function | The callback is called with arguments <ul><li>An error, if any</li><li>The result. This is an object of the raw result from Mapbox.</li></ul>
_Returns_: the geocoder object. The return value of this function is not useful - you must use a callback to get results.
#### Reverse Geocoding Examples
```
var geocoder = L.cedarmaps.geocoder('cedarmaps.streets');
geocoder.reverseQuery({lat: 35.754592526442465, lng: 51.401896476745605}, function(){});
```
# Building SDK
Requires [node.js](http://nodejs.org/) installed on your system.
Grunt makes use of [Browserify](http://browserify.org/) under the hood to build the project while resolving dependencies.
......@@ -52,7 +145,7 @@ grunt build
Built files are copied into `dist/` folder according to current SDK `version`. (See `package.json`)
Note that every time you pull new changes from repository, you should run `grunt build`.
# Updating
# Updating SDK
## Updating mapbox.js submodule
The mapbox.js SDK is updated from time to time and obviously cedarmaps.js also needs to be synced. The submodule is residing in `/mapbox.js` folder and points to a specific mapbox.js tag (not the HEAD, of course).
......
.leaflet-control-mapbox-geocoder.active .leaflet-control-mapbox-geocoder-wrap {
width: 250px;
}
.leaflet-control-mapbox-geocoder .leaflet-control-mapbox-geocoder-form input {
width: 250px;
}
.leaflet-control-mapbox-geocoder-results {
width: 250px;
}
\ No newline at end of file
......@@ -3,11 +3,14 @@
<head>
<meta charset=utf-8 />
<title>Reverse Geocoding Testing Environment</title>
<title>CedarMaps - Sample Geocoder Control</title>
<script src='../dist/v1.0.1/cedarmaps.js'></script>
<script src='../dist/v1.1.0/cedarmaps.js'></script>
<script src='../access-token.js'></script>
<link href='../dist/v1.0.1/cedarmaps.css' rel='stylesheet' />
<link href='../dist/v1.1.0/cedarmaps.css' rel='stylesheet' />
<link href='css/styles.css' rel='stylesheet' />
<style>
body {
......
......@@ -3,11 +3,11 @@
<head>
<meta charset=utf-8 />
<title>CedarMaps Web SDK Demos</title>
<title>CedarMaps - Simple Map Load</title>
<script src='../dist/v1.0.1/cedarmaps.js'></script>
<script src='../dist/v1.1.0/cedarmaps.js'></script>
<script src='../access-token.js'></script>
<link href='../dist/v1.0.1/cedarmaps.css' rel='stylesheet' />
<link href='../dist/v1.1.0/cedarmaps.css' rel='stylesheet' />
<style>
body {
......
......@@ -3,11 +3,11 @@
<head>
<meta charset=utf-8 />
<title>Reverse Geocoding Testing Environment</title>
<title>CedarMaps - Reverse Geocoding Sample</title>
<script src='../dist/v1.0.1/cedarmaps.js'></script>
<script src='../dist/v1.1.0/cedarmaps.js'></script>
<script src='../access-token.js'></script>
<link href='../dist/v1.0.1/cedarmaps.css' rel='stylesheet' />
<link href='../dist/v1.1.0/cedarmaps.css' rel='stylesheet' />
<style>
body {
......
......@@ -2,7 +2,7 @@
"author": "CedarStudios",
"name": "cedarmaps.js",
"description": "CedarMaps javascript API built on top of mapbox.js",
"version": "1.0.1",
"version": "1.1.0",
"homepage": "http://www.cedarmaps.com/",
"repository": {
"type": "git",
......@@ -10,10 +10,11 @@
},
"main": "src/index.js",
"dependencies": {
"leaflet": "0.7.3",
"mustache": "0.7.3",
"corslite": "0.0.6",
"sanitize-caja": "0.1.2"
"isarray": "0.0.1",
"leaflet": "0.7.7",
"mustache": "0.7.3",
"sanitize-caja": "0.1.3"
},
"devDependencies": {
"browserify": "^6.3.2",
......
......@@ -7,18 +7,17 @@ module.exports = function(path, accessToken) {
accessToken = accessToken || L.cedarmaps.accessToken;
if (!accessToken && config.REQUIRE_ACCESS_TOKEN) {
// TODO: Change the urls for getting access token
throw new Error('An API access token is required to use cedarmaps.js. ' +
'See https://www.mapbox.com/mapbox.js/api/v' + version + '/api-access-tokens/');
}
var url = ('https:' === document.location.protocol || config.FORCE_HTTPS) ? config.HTTPS_URL : config.HTTP_URL;
var url = (document.location.protocol === 'https:' || config.FORCE_HTTPS) ? config.HTTPS_URL : config.HTTP_URL;
url = url.replace(/\/v4$/, '');
url += path;
url += url.indexOf('?') !== -1 ? '&access_token=' : '?access_token=';
if (config.REQUIRE_ACCESS_TOKEN) {
if (accessToken[0] === 's') {
// TODO: Change the urls for getting access token
throw new Error('Use a public access token (pk.*) with Mapbox.js, not a secret access token (sk.*). ' +
'See https://www.mapbox.com/mapbox.js/api/v' + version + '/api-access-tokens/');
}
......
'use strict';
var util = require('../mapbox.js/src/util'),
urlhelper = require('./url'),
var isArray = require('isarray'),
util = require('../mapbox.js/src/util'),
format_url = require('./format_url'),
//feedback = require('./feedback'),
request = require('./request');
// Low-level geocoding interface - wraps specific API calls and their
// return values.
module.exports = function(url, options) {
if (!options) options = {};
var geocoder = {};
util.strict(url, 'string');
if (url.indexOf('/') === -1) {
url = urlhelper('/geocode/' + url + '/{query}.json', options && options.accessToken);
url = format_url('/geocode/' + url + '/{query}.json', options.accessToken, 5);
}
function roundTo(latLng, precision) {
var mult = Math.pow(10, precision);
latLng.lat = Math.round(latLng.lat * mult) / mult;
latLng.lng = Math.round(latLng.lng * mult) / mult;
return latLng;
}
geocoder.getURL = function() {
......@@ -22,24 +30,58 @@ module.exports = function(url, options) {
};
geocoder.queryURL = function(_) {
var query;
var isObject = !(isArray(_) || typeof _ === 'string'),
query = isObject ? _.query : _;
if (typeof _ !== 'string') {
if (isArray(query)) {
var parts = [];
for (var i = 0; i < _.length; i++) {
parts[i] = encodeURIComponent(_[i]);
for (var i = 0; i < query.length; i++) {
parts[i] = encodeURIComponent(query[i]);
}
query = parts.join(';');
} else {
query = encodeURIComponent(_);
query = encodeURIComponent(query);
}
//feedback.record({ geocoding: query });
var url = L.Util.template(geocoder.getURL(), {query: query});
/*
* Handling cedarmaps' API options for geocoding
*/
if (isObject && _.limit) {
url += '&limit=' + _.limit;
}
if (isObject && _.distance) {
url += '&distance=' + _.distance;
}
if (isObject && _.ne && _.sw) {
url += '&ne=' + _.ne + '&sw=' + _.sw;
}
if (isObject && _.type) {
if (isArray(_.type)) {
url += '&type=' + _.type.join(',');
} else {
url += '&type=' + _.type;
}
}
if (isObject && _.proximity) {
var proximity = roundTo(L.latLng(_.proximity), 3);
url += '&proximity=' + proximity.lng + ',' + proximity.lat;
}
//feedback.record({geocoding: query});
return L.Util.template(geocoder.getURL(), {query: query});
return url;
};
geocoder.query = function(_, callback) {
util.strict(callback, 'function');
request(geocoder.queryURL(_), function(err, json) {
if (json && (json.length || json.results)) {
callback(null, json);
......
......@@ -22,7 +22,9 @@ var GeocoderControl = L.Control.extend({
},
setURL: function(_) {
this.geocoder = geocoder(_, {accessToken: this.options.accessToken});
this.geocoder = geocoder(_, {
accessToken: this.options.accessToken
});
return this;
},
......@@ -51,7 +53,7 @@ var GeocoderControl = L.Control.extend({
}
},
_closeIfOpen: function(e) {
_closeIfOpen: function() {
if (L.DomUtil.hasClass(this._container, 'active') &&
!this.options.keepOpen) {
L.DomUtil.removeClass(this._container, 'active');
......@@ -138,7 +140,14 @@ var GeocoderControl = L.Control.extend({
for (var i = 0, l = Math.min(features.length, 10); i < l; i++) {
var feature = features[i];
var name = feature.name;
var address = feature.address;
var addressComponents = [];
//if (feature.components.country) addressComponents.push(feature.components.country);
if (feature.components.city) addressComponents.push(feature.components.city);
if (feature.components.districts[0]) addressComponents.push(feature.components.districts[0]);
if (feature.components.localities[0]) addressComponents.push(feature.components.localities[0]);
var address = addressComponents.join('، ');
if (!name.length) continue;
var r = L.DomUtil.create('a', '', this._results);
......@@ -184,13 +193,19 @@ var GeocoderControl = L.Control.extend({
L.DomEvent.preventDefault(e);
if (this._input.value === '') return this._updateSubmit();
L.DomUtil.addClass(this._container, 'searching');
this.geocoder.query(this._input.value, this._updateSubmit);
this.geocoder.query({
query: this._input.value,
proximity: this.options.proximity ? this._map.getCenter() : false
}, this._updateSubmit);
},
_autocomplete: function(e) {
_autocomplete: function() {
if (!this.options.autocomplete) return;
if (this._input.value === '') return this._updateAutocomplete();
this.geocoder.query(this._input.value, this._updateAutocomplete);
this.geocoder.query({
query: this._input.value,
proximity: this.options.proximity ? this._map.getCenter() : false
}, this._updateAutocomplete);
}
});
......
'use strict';
require('../mapbox.js/src/index');
require('./cedarmaps');
......@@ -13,18 +13,19 @@ module.exports = function(url, callback) {
if (!('withCredentials' in new window.XMLHttpRequest())) {
// XDomainRequest in use; doesn't support cross-protocol requests
return document.location.protocol;
} else if ('https:' === protocol || 'https:' === document.location.protocol || config.FORCE_HTTPS) {
} else if (protocol === 'https:' || document.location.protocol === 'https:' || config.FORCE_HTTPS) {
return 'https:';
} else {
return 'http:';
}
});
return corslite(url, onload);
function onload(err, resp) {
if (!err && resp) {
resp = JSON.parse(resp.responseText);
}
callback(err, resp);
}
return corslite(url, onload);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment