Skip to content

Commit

Permalink
Merge pull request #29 from conveyal/rename-to-lonlat
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Rename to lonlat
  • Loading branch information
trevorgerhardt authored Jan 31, 2017
2 parents ad42db8 + 043b455 commit 990c5f7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 53 deletions.
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
sudo: false
language: node_js
cache:
directories:
- node_modules
yarn: true
notifications:
email: false
node_js:
- '6'
before_install:
- npm i -g codecov
before_script:
- npm prune
script:
- npm run lint
- npm run cover
- yarn run lint
- yarn run cover
- codecov
after_success:
- npm run semantic-release
- yarn run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,49 @@ Isomorphic Mapzen search for reuse across our JavaScript libraries. Get an API k

## API

### `search(apiKey, searchText, options = {})`
### `search({apiKey, text, ...options})`

```js
import {search} from 'isomorphic-mapzen-search'
search(MAPZEN_API_KEY, '1301 U Street NW, Washington, DC', {
search({
apiKey: MAPZEN_API_KEY,
text: '1301 U Street NW, Washington, DC',
focusPoint: {lat: 39.7691, lon: -86.1570},
boundary: {
country: 'US',
maxLatlng: maxLatlng,
minLatlng: minLatlng
rect: {
minLon: minLon,
minLat: minLat,
maxLon: maxLon,
maxLat: maxLat
},
circle: {
centerPoint: centerLonLat,
radius: 35 // kilometers
}
},
circle: {
latlng: centerLatlng,
radius: 35 // kilometers
},
focusLatlng: {lat: 39.7691, lng: -86.1570},
format: false // keep as returned GeoJSON
}).then(geojson => {
}).then((geojson) => {
console.log(geojson)
}).catch(err => {
}).catch((err) => {
console.error(err)
})
```

### `reverse(apiKey, latlng, options = {})`
### `reverse({apiKey, point, format})`

```js
import {reverse} from 'isomorphic-mapzen-search'
reverse(MAPZEN_API_KEY, {
lat: 39.7691,
lng: -86.1570
}, {
reverse({
apiKey: MAPZEN_API_KEY,
point: {
lat: 39.7691,
lng: -86.1570
},
format: true
}).then(json => {
}).then((json) => {
console.log(json[0].address)
}).catch(err => {
}).catch((err) => {
console.error(err)
})
```
8 changes: 4 additions & 4 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('search', () => {
.get(/v1\/search/)
.reply(200, mockSearchResult)

const result = await geocoder.search(mockKey, searchQuery)
const result = await geocoder.search({apiKey: mockKey, text: searchQuery})
expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104)
})

Expand All @@ -24,7 +24,7 @@ describe('search', () => {
.get(/v1\/search/)
.reply(200, mockSearchResult)

const result = await geocoder.search(mockKey, searchQuery, { format: true })
const result = await geocoder.search({apiKey: mockKey, format: true, text: searchQuery})
expect(result).toMatchSnapshot()
expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA')
})
Expand All @@ -38,7 +38,7 @@ describe('reverse', () => {
.get(/v1\/reverse/)
.reply(200, mockReverseResult)

const result = await geocoder.reverse(mockKey, reverseQuery)
const result = await geocoder.reverse({apiKey: mockKey, point: reverseQuery})
expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104)
})

Expand All @@ -47,7 +47,7 @@ describe('reverse', () => {
.get(/v1\/reverse/)
.reply(200, mockReverseResult)

const result = await geocoder.reverse(mockKey, reverseQuery, { format: true })
const result = await geocoder.reverse({apiKey: mockKey, format: true, point: reverseQuery})
expect(result).toMatchSnapshot()
expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA')
})
Expand Down
60 changes: 36 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const mapzenUrl = 'https://search.mapzen.com/v1'
const searchUrl = `${mapzenUrl}/search`
const reverseUrl = `${mapzenUrl}/reverse`

export function search (apiKey, text, {
export function search ({
apiKey,
boundary,
focusLatlng,
focusPoint,
format,
size = 10,
sources = 'gn,oa,osm,wof'
} = {}) {
sources = 'gn,oa,osm,wof',
text
}) {
if (!text) return Promise.resolve([])

const query = {
Expand All @@ -22,46 +24,56 @@ export function search (apiKey, text, {
text
}

if (focusLatlng) {
const {lat, lon} = lonlat(focusLatlng)
if (focusPoint) {
const {lat, lon} = lonlat(focusPoint)
query['focus.point.lat'] = lat
query['focus.point.lon'] = lon
}

if (boundary) {
if (boundary.country) query['boundary.country'] = boundary.country
if (boundary.rect) {
const min = lonlat(boundary.rect.minLatlng)
const max = lonlat(boundary.rect.maxLatlng)
query['boundary.rect.min_lat'] = min.lat
query['boundary.rect.min_lon'] = min.lon
query['boundary.rect.max_lat'] = max.lat
query['boundary.rect.max_lon'] = max.lon
query['boundary.rect.min_lat'] = boundary.rect.minLat
query['boundary.rect.min_lon'] = boundary.rect.minLon
query['boundary.rect.max_lat'] = boundary.rect.maxLat
query['boundary.rect.max_lon'] = boundary.rect.maxLon
}
if (boundary.circle) {
const {lat, lon} = lonlat(boundary.circle.latlng)
const {lat, lon} = lonlat(boundary.circle.centerPoint)
query['boundary.circle.lat'] = lat
query['boundary.circle.lon'] = lon
query['boundary.circle.radius'] = boundary.circle.radius
}
}

return run(searchUrl, query, format)
return run({format, query})
}

export function reverse (apiKey, latlon, {format} = {}) {
const {lon, lat} = lonlat(latlon)
return run(reverseUrl, {
api_key: apiKey,
'point.lat': lat,
'point.lon': lon
}, format)
export function reverse ({
apiKey,
format,
point
}) {
const {lon, lat} = lonlat(point)
return run({
format,
query: {
api_key: apiKey,
'point.lat': lat,
'point.lon': lon
},
url: reverseUrl
})
}

function run (url, query, format) {
function run ({
format = false,
query,
url = searchUrl
}) {
return fetch(`${url}?${qs.stringify(query)}`)
.then(res => res.json())
.then(json => { return json && json.features && format ? json.features.map(split) : json })
.then((res) => res.json())
.then((json) => { return json && json.features && format ? json.features.map(split) : json })
}

function split ({
Expand Down

0 comments on commit 990c5f7

Please sign in to comment.