Git Product home page Git Product logo

open-location-code-swift's Introduction

Open Location Code for Swift and Objective-C

Build Status Carthage compatible

Convert between decimal degree coordinates and Open Location Codes. Shorten and recover Open Location Codes for a given reference location.

This repository is the Swift implementation of Open Location Code. It supports Swift and Objective-C projects on iOS, macOS, tvOS and watchOS, and Swift projects on Linux. The master repository has Open Location Code support for many other languages.

About Open Location Codes

Open Location Codes are short, 10-11 character codes that can be used instead of street addresses. The codes can be generated and decoded offline, and use a reduced character set that minimises the chance of codes including words.

Codes are able to be shortened relative to a nearby location. This means that in many cases, only four to seven characters of the code are needed. To recover the original code, the same location is not required, as long as a nearby location is provided.

Codes represent rectangular areas rather than points, and the longer the code, the smaller the area. A 10 character code represents a 13.5x13.5 meter area (at the equator. An 11 character code represents approximately a 2.8x3.5 meter area.

Two encoding algorithms are used. The first 10 characters are pairs of characters, one for latitude and one for latitude, using base 20. Each pair reduces the area of the code by a factor of 400. Only even code lengths are sensible, since an odd-numbered length would have sides in a ratio of 20:1. At position 11, the algorithm changes so that each character selects one position from a 4x5 grid. This allows single-character refinements.

Supported Environments

This library is provided as a Swift and Objective-C Cocoa Framework for iOS, macOS, tvOS and watchOS, and as a pure Swift module for macOS and Linux.

Swift Versions

  • Versions 3.x of Open Location Code for Swift are designed for Swift 5.0.
  • Versions 2.x of Open Location Code for Swift are designed for Swift 4.0.
  • Versions 1.x of Open Location Code for Swift were designed for Swift 3.2, but are no longer maintained.

Building

Cocoa Framework

To build the Framework:

xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_iOS -configuration Release
xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_macOS -configuration Release
xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_tvOS -configuration Release
xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_watchOS -configuration Release

Or, if you have Carthage installed:

carthage build --no-skip-current

Testing the framework:

xcodebuild test -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_macOS -destination 'platform=OS X,arch=x86_64'

Swift Module

To build the pure Swift module:

swift build

Testing the pure Swift module:

swift test

A Dockerfile is included to build and run the pure Swift module in a Linux container:

docker build .

Swift Code Example

import OpenLocationCode

// Encode a location with default code length.
if let code = OpenLocationCode.encode(latitude: 37.421908,
                                      longitude: -122.084681) {
  print("Open Location Code: \(code)")
}

// Encode a location with specific code length.
if let code10Digit = OpenLocationCode.encode(latitude: 37.421908,
                                             longitude: -122.084681,
                                             codeLength: 10) {
  print("Open Location Code: \(code10Digit)")
}

// Decode a full code:
if let coord = OpenLocationCode.decode("849VCWC8+Q48") {
  print("Center is \(coord.latitudeCenter), \(coord.longitudeCenter)")
}

// Attempt to trim the first characters from a code:
if let shortCode = OpenLocationCode.shorten(code: "849VCWC8+Q48",
                                            latitude: 37.4,
                                            longitude: -122.0) {
  print("Short code: \(shortCode)")
}

// Recover the full code from a short code:
if let fullCode = OpenLocationCode.recoverNearest(shortcode: "CWC8+Q48",
                                                  referenceLatitude: 37.4,
                                                  referenceLongitude: -122.0) {
  print("Recovered full code: \(fullCode)")
}

Objective-C Code Example

@import OpenLocationCode;

// ...

// Encode a location with default code length.
NSString *code = [OLCConverter encodeLatitude:37.421908
                                    longitude:-122.084681];
NSLog(@"Open Location Code: %@", code);

// Encode a location with specific code length.
NSString *code10Digit = [OLCConverter encodeLatitude:37.421908
                                           longitude:-122.084681
                                          codeLength:10];
NSLog(@"Open Location Code: %@", code10Digit);

// Decode a full code:
OLCArea *coord = [OLCConverter decode:@"849VCWC8+Q48"];
NSLog(@"Center is %.6f, %.6f", coord.latitudeCenter, coord.longitudeCenter);

// Attempt to trim the first characters from a code:
NSString *shortCode = [OLCConverter shortenCode:@"849VCWC8+Q48"
                                       latitude:37.4
                                      longitude:-122.0];
NSLog(@"Short Code: %@", shortCode);

// Recover the full code from a short code:
NSString *recoveredCode = [OLCConverter recoverNearestWithShortcode:@"CWC8+Q48"
                                                  referenceLatitude:37.4
                                                 referenceLongitude:-122.1];
NSLog(@"Recovered Full Code: %@", recoveredCode);

Testing the CI Locally

To run the Travis CI tests locally, install:

gem install wwtd
gem install xcpretty

And run:

wwtd

Your version of Xcode and macOS will need to match the Travis osx_image configuration.

open-location-code-swift's People

Contributors

williamdenniss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

open-location-code-swift's Issues

How to use this framework?

Hi,
Problems with using this at objective-c project on High Sierra, Xcode 9.4 beta:

ld: warning: ignoring file .../OpenLocationCode.framework/OpenLocationCode, file was built for arm64 which is not the architecture being linked (x86_64): .../OpenLocationCode.framework/OpenLocationCode
Undefined symbols for architecture x86_64:
"OBJC_CLASS$__TtC16OpenLocationCode7OLCArea", referenced from:
objc-class-ref in DetailViewController.o
"OBJC_CLASS$__TtC16OpenLocationCode12OLCConverter", referenced from:
objc-class-ref in DetailViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Cannot obtain full code with shorten code only!

I am unable to obtain the full code with the short code only and I think it is awful!

OpenLocationCode.recoverNearest(shortcode: "CWC8+Q48", referenceLatitude: 37.4, referenceLongitude: -122.0)

I would have loved to use just the short code following your comment below via curbmap/OpenLocationCode-swift#1

3/ I also included ports of the recover nearest implementation. I like the short-code feature of OLC, it's useful to be able to give someone in the same city as you a shorter 6 digit code for locations within the city ☺️. Some apps like Google Maps already highlight the short code characters to speed up communication time (e.g. when you're telling someone where you are on the phone).

I know very well that many (if not all) people cannot memorize their coordinates. All they need is memorize/capture either the full or the short code and give same to others to get their address.

My second issue is the fact that OpenLocationCode.decode(code: String) seems to be producing wrong coordinates. Take the following as a case study:

Current Location


latitude CLLocationDegrees 6.6073411563472861
longitude CLLocationDegrees 3.3226713258804024
code String? “6FR5J84F+”

Decoded location using the (codeLength: 8) full code generated above


latitudeLo Double 6.6049999999999898
longitudeLo Double 3.3225000000000193

latitudeHi Double 6.6074999999999875
longitudeHi Double 3.3250000000000171

latitudeCenter Double 6.6062499999999886
longitudeCenter Double 3.3237500000000182

The output didn't match the original (Current Location), thereby marking a different location on the map when used.

Please help!

Floating point precision issues for Swift

Hello from the main Open Location Code repo!

We're currently working on fixing floating point precision issues with the various implementations (see here). For the implementations not directly in the main repo, we're asking for the owners to update their code accordingly. Would you be able to do the following when you get a chance?

  1. Run the new test cases in test_data/encoding.csv.
  2. If they fail, fix your implementation to properly handle the floating point precision issues. I'll post again when we have a commit that you can use for reference.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.