# Open Location Code JavaScript API
This is the JavaScript implementation of the Open Location Code API.
The library file is in `src/openlocationcode.js`. There is also a
minified version, and both are also available using the following CDNs:
* [jsDelivr](https://www.jsdelivr.com)
* https://cdn.jsdelivr.net/openlocationcode/latest/openlocationcode.js
* https://cdn.jsdelivr.net/openlocationcode/latest/openlocationcode.min.js
* [cdnjs](https://cdnjs.com/)
* https://cdnjs.cloudflare.com/ajax/libs/openlocationcode/1.0.3/openlocationcode.js
* https://cdnjs.cloudflare.com/ajax/libs/openlocationcode/1.0.3/openlocationcode.min.js
## Releasing
Once changes have been made and merged, start a new PR:
* run `gulp minify` to update the minified Javascript in `src`.
* update the `version` tag in the `package.json` file
To update the CDNs, you will have to add a new release tag. Note that release
tags are applied globally to the repository, so if you are making a change
across multiple implementations, consider waiting until all are updated before
adding the release tag.
# Tests
Unit tests require [gulp](https://www.npmjs.com/package/gulp),
[karma](https://karma-runner.github.io) and
[jasmine](https://jasmine.github.io).
Execute the tests with `sh test/run_tests.sh`. This will install the
dependencies, run `eslint` and then run the tests as long as there were no
eslint errors.
Unit tests are automatically run on pull and push requests and visible at
https://travis-ci.org/google/open-location-code.
# Examples
Example web pages illustrating converting map clicks to Open Location Codes,
and using Googles Maps API to extend place codes to full codes are in the
`examples/` directory.
More examples are on [jsfiddle](https://jsfiddle.net/user/openlocationcode/fiddles/).
# Public Methods
The following are the four public methods and one object you should use. All the
other methods in the code should be regarded as private and not called.
## encode()
```javascript
OpenLocationCode.encode(latitude, longitude, codeLength) → {string}
```
Encode a location into an Open Location Code.
**Parameters:**
| Name | Type | Description |
|------|------|-------------|
| `latitude` | `number` | The latitude in signed decimal degrees. Values less than -90 will be clipped to -90, values over 90 will be clipped to 90. |
| `longitude` | `number` | The longitude in signed decimal degrees. This will be normalised to the range -180 to 180. |
| `codeLength` | `number` | The desired code length. If omitted, `OpenLocationCode.CODE_PRECISION_NORMAL` will be used. For precision `OpenLocationCode.CODE_PRECISION_EXTRA` is recommended. |
**Returns:**
The code for the location.
**Exceptions:**
If any of the passed values are not numbers, an exception will be thrown.
## decode()
```javascript
OpenLocationCode.decode(code) → {OpenLocationCode.CodeArea}
```
Decodes an Open Location Code into its location coordinates.
**Parameters:**
| Name | Type | Description |
|------|------|-------------|
| `code` | `string` | The code to decode. |
**Returns:**
The `OpenLocationCode.CodeArea` object.
**Exceptions:**
If the passed code is not a valid full code, an exception will be thrown.
## shorten()
```javascript
OpenLocationCode.shorten(code, latitude, longitude) → {string}
```
Remove characters from the start of an OLC code.
This uses a reference location to determine how many initial characters
can be removed from the OLC code. The number of characters that can be
removed depends on the distance between the code center and the reference
location.
**Parameters:**
| Name | Type | Description |
|------|------|-------------|
| `code` | `string` | The code to shorten. |
| `latitude` | `number` | The latitude of the reference location. |
| `longitude` | `number` | The longitude of the reference location. |
**Returns:**
The code, shortened as much as possible that it is still the closest matching
code to the reference location.
**Exceptions:**
If the code is not a valid full code, or the latitude or longitude are not
numbers, an exception will be thrown.
## recoverNearest()
```javascript
OpenLocationCode.recoverNearest(shortCode, referenceLatitude, referenceLongitude) → {string}
```
Recover the nearest matching code to a specified location.
This is the counterpart to `OpenLocationCode.shorten()`. This recovers the
nearest matching full code to the reference location.
**Parameters:**
| Name | Type | Description |
|------|------|-------------|
| `shortCode` | `string` | The code to recover. |
| `referenceLatitude` | `number` | The latitude of the reference location. |
| `referenceLongitude` | `number` | The longitude of the reference location. |
**Returns:**
The nearest matching full code to the reference location.
**Exceptions:**
If the short code is not valid, or the reference position values are not
numbers, an exception will be thrown.
## CodeArea
```javascript
OpenLocationCode.CodeArea(latitudeLo, longitudeLo, latitudeHi, longitudeHi, codeLength) → {OpenLocationCode.CodeAre}
```
The `OpenLocationCode.CodeArea` class is used to return the area represented by
a code. Because codes are areas, not points, this gives the coordinates of the
south-west and north-east corners, the center, and the length of the code.
You can convert from a code to an area and back again like this:
```javascript
var a = '796RWF8Q+WF';
var area = OpenLocationCode.decode(a);
var original_code = OpenLocationCode.encode(area.latitudeCenter, area.longitudeCenter, area.codeLength);
```
**Attributes:**
| Name | Type | Description |
|------|------|-------------|
| `latitudeLo` | `number` | The latitude of the south-west corner. |
| `longitudeLo` | `number` | The longitude of the south-west corner. |
| `latitudeHi` | `number` | The latitude of the north-east corner. |
| `longitudeHi` | `number` | The longitude of the north-east corner. |
| `latitudeCenter` | `number` | The latitude of the center. |
| `longitudeCenter` | `number` | The longitude of the center. |
| `codeLength` | `number` | The length of the code that generated this area. |
没有合适的资源?快使用搜索试试~ 我知道了~
open-location-code.zip
共318个文件
png:60个
xml:53个
java:35个
需积分: 5 0 下载量 181 浏览量
2023-08-18
09:44:49
上传
评论
收藏 2.16MB ZIP 举报
温馨提示
open-location-code
资源推荐
资源详情
资源评论
收起资源包目录
open-location-code.zip (318个子文件)
olc_definition.adoc 17KB
comparison.adoc 220B
OpenLocationCode.bas 28KB
gradlew.bat 2KB
.bazelrc 517B
BUILD 3KB
BUILD 965B
BUILD 819B
BUILD 805B
BUILD 362B
gtest.BUILD 300B
BUILD 107B
olc.c 18KB
shared.c 4KB
bm-c.c 2KB
example.c 2KB
openlocationcode.cc 18KB
openlocationcode_test.cc 11KB
openlocationcode_test.cc 10KB
bm-cpp.cc 2KB
openlocationcode_example.cc 2KB
codearea.cc 1KB
.clang-format 3KB
.clang-format 3KB
examples.css 1KB
encoding.csv 5KB
shortCodeTests.csv 1KB
decoding.csv 1KB
validityTests.csv 913B
open_location_code.dart 21KB
decode_test.dart 2KB
encode_test.dart 2KB
benchmark_test.dart 1KB
short_code_test.dart 1KB
validity_test.dart 1KB
clip_latitude_test.dart 918B
open_location_code.dart 653B
utils.dart 648B
compute_precision_test.dart 274B
open-location-code.gemspec 840B
.gitignore 486B
.gitignore 293B
.gitignore 83B
.gitignore 53B
.gitignore 40B
.gitignore 39B
.gitignore 33B
.gitignore 33B
.gitignore 19B
.gitignore 12B
olc.go 7KB
olc_test.go 7KB
image.go 7KB
projection.go 6KB
encode.go 4KB
shorten.go 4KB
geojson.go 4KB
gridserver.go 3KB
decode.go 3KB
tileref.go 3KB
geojson_test.go 2KB
main.go 2KB
projection_test.go 2KB
gen.go 2KB
olc_gofuzz.go 1KB
image_test.go 1KB
csv_to_json.go 1KB
build.gradle 1KB
build.gradle 1KB
settings.gradle 19B
gradlew 5KB
openlocationcode.h 5KB
olc_private.h 3KB
olc.h 2KB
codearea.h 758B
example3.html 11KB
example1.html 9KB
example.html 6KB
example2.html 5KB
openlocationcode_android.iml 955B
gradle-wrapper.jar 52KB
OpenLocationCode.java 24KB
Locality.java 14KB
LocationProvider.java 13KB
MainActivity.java 9KB
SearchView.java 8KB
MyMapView.java 8KB
MainPresenter.java 7KB
CodePresenter.java 6KB
SearchPresenter.java 5KB
MapPresenter.java 5KB
DecodingTest.java 4KB
OpenLocationCodeUtil.java 4KB
WelcomeActivity.java 3KB
LocationUtil.java 3KB
DirectionView.java 3KB
CodeView.java 3KB
DirectionUtil.java 3KB
ShorteningTest.java 3KB
ValidityTest.java 3KB
共 318 条
- 1
- 2
- 3
- 4
资源评论
hello_中年人
- 粉丝: 7
- 资源: 324
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功