Coder Social home page Coder Social logo

abzico / sphericalmercator-swift Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 3.0 7 KB

SphericalMercator in Swift implementation from original JS implementation at https://github.com/mapbox/sphericalmercator/blob/master/sphericalmercator.js

License: MIT License

Swift 100.00%
mapbox swift sphericalmercator coordinate-systems coordinate

sphericalmercator-swift's Introduction

donate button

SphericalMercator-swift

SphericalMercator in Swift implementation from original JS implementation at sphericalmercator.js

What's for?

Working with Mapbox for iOS SDK. If you need to implement chunk/grid system that has equal size all across the map, then using Spherical Mercator Coordinate will help achieve that.

Initially Mapbox uses WGS84 coordinate system, and with the fact that you can just define fixed distance values of latitude/longitude to define your tile to achive the sqaured grid visually on Mapbox. Spherical Mercator Coordinate system will help.

You can read here for more context for our problem on trying to achieve such goal.

After applying with Spherical Mercator, top-left most is original with tile index of 0,0 and grows towards bottom-right.

How to Use?

  • Creating an instance of SphericalMercator with default tile size of 256 * 256
let sphericalMercator = SphericalMercator()
  • Creating an instance of SphericalMercator with set tile size
let sphericalMercator = SphericalMercator(size: 128)

To my test, tilesize seems not to get into effect. Only zoom level is the factor. This is one point to look at.

  • Converting longitude/latitude location to tile index
let bounds = sphericalMercator.xyz(bbox: [location.longitude, location.latitude, location.longitude, location.latitude], zoom: 22)
let tileIndex = (x: Int(floor(bounds["minX"]!)), y: Int(floor(bounds["maxY"]!)))

location is CLLocationCoordinate2D and is your known information. Remember to input with longitude first before latitude. Zoom level is a set value you intend to use across the app. If such value changes, it will split the map into various number of tiles across the map. Mapbox supports maximum of zoom level of 22, thus it will generate the largest total number of tiles with this number (4194303 (width) * 4194303 (height) =~ 17 MM tiles).

or you can do this

let bounds = sphericalMercator.xyz(bbox: [location.longitude, location.latitude, 0.0, 0.0], zoom: 22)
let tileIndex = (x: Int(floor(bounds["minX"]!)), y: Int(floor(bounds["maxY"]!)))

it will give you the same tile index.

  • Converting from tile index to bounding box of longitude/latitude
let bbox = sphericalMercator.bbox(x: Double(x), y: Double(y), zoom: 22)

bbox is in array of Double in form [west, south, east, north]. You can further use this bounding box to render grid line on top of Mapbox by creating MGLPolygonLine from this information.

License

MIT, abzi.co

sphericalmercator-swift's People

Contributors

haxpor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.