GPS Hardware basics for mobile developers.
4.7 (3)

Click to rate this post!
[Total: 3 Average: 4.7]

Mobile software engineers (iOS and Android), are normally not familiar with how GPS works, instead of just getting Lat/Lon readings, and doing geo operations with it, why not become familiar with how GPS works? πŸ€“

This will not be a lengthy article, it will be a chat between an iOS developer (Alex πŸ‘¨πŸ»β€πŸ’») and an electrical engineer (Sarah πŸ‘©πŸΌβ€πŸ’»).

πŸ‘¨πŸ»β€πŸ’»: So what does GPS stand for?
πŸ‘©πŸΌβ€πŸ’»: It stands for (Global Positioning System).

πŸ‘¨πŸ»β€πŸ’»: Who created it? and what for?
πŸ‘©πŸΌβ€πŸ’»: The GPS project was launched in the USA back in 1973 due to limitations of old navigation systems.

πŸ‘¨πŸ»β€πŸ’»: I know it works without internet, but do I need cellular service to use GPS?
πŸ‘©πŸΌβ€πŸ’»: No.

πŸ‘¨πŸ»β€πŸ’»: How come it works without internet or cellular service?
πŸ‘©πŸΌβ€πŸ’»: You get readings from satellites, there are about 24 artificial satellites in 6 orbits.

πŸ‘¨πŸ»β€πŸ’»: So a mobile needs to connect to all of these?
πŸ‘©πŸΌβ€πŸ’»: Of course not, when you are stationary, you need to be exposed to 3 of them, when you are moving, you will need to be exposed to 4.

GPS Satellites animation (Wikipedia)

πŸ‘¨πŸ»β€πŸ’»: So how come it identifies me? and send me data?
πŸ‘©πŸΌβ€πŸ’»: The Satellites don’t identify you, they only emit synchronous pulses all the time everywhere.

πŸ‘¨πŸ»β€πŸ’»: And how does my mobile give me back the (latitude, longitude, and altitude)?
πŸ‘©πŸΌβ€πŸ’»: It compares the receive time of these pulses from each satellite, and use calculations to determine a point on earth, since the distance between these satellites is constant, and they have atomic clocks, the calculations will not be difficult.

πŸ‘¨πŸ»β€πŸ’»: The service is totally free, and I don’t have any subscription for GPS, how?
πŸ‘©πŸΌβ€πŸ’»: GPS is not the only service for (Global Navigation Satellite Systems), there are many like (GLONASS, BeiDou, Galileo…), there are other commercial solutions that I don’t know much about, there are a lot of details, I heard retail GPS receivers are designed to not work if the tracked object is moving fastly, you get the idea 🧐?
πŸ‘¨πŸ»β€πŸ’»: ah! yes.

BeiDou doesn’t have full earth coverage.

πŸ‘¨πŸ»β€πŸ’»: What is the error margin?
πŸ‘©πŸΌβ€πŸ’»: It’s variable, but you can say between 15 to 50 meters, some commercial systems use other inertial systems to give more accurate estimations.

πŸ‘¨πŸ»β€πŸ’»: What is the minimum detectable value?
πŸ‘©πŸΌβ€πŸ’»: You mean the resolution? theoretically, as far as I know, it’s one inch, but practically it’s about 3 meters.

πŸ‘¨πŸ»β€πŸ’»: I once tried to use the GPS inside a big hospital, it didn’t serve any purpose, the readings were not accurate.
πŸ‘©πŸΌβ€πŸ’»: GPS does not work indoors.
πŸ‘¨πŸ»β€πŸ’»: But I saw some readings on my maps application.
πŸ‘©πŸΌβ€πŸ’»: it’s the last point that was read, some devices like Huawei also augment (Accel/Gyro) sensor data, to mimic a basic INS to give your readings inside buildings, but it’s not reliable.

πŸ‘¨πŸ»β€πŸ’»: And what is used for indoor navigation systems?
πŸ‘©πŸΌβ€πŸ’»: They use beacons and Bluetooth and other technologies, read about apple air tags!

πŸ‘¨πŸ»β€πŸ’»: You mentioned sensors, why can’t we use the basic sensors like accelerometer/gyroscope of the mobile to calculate the position?
πŸ‘©πŸΌβ€πŸ’»: when you “integrate” the acceleration twice, the error will explode fastly, and it will become useless in a short time of movement, even if this works, this will not give you an absolute position, and you have to deal with drifting and gimbal lock and a lot of complexities.

πŸ‘¨πŸ»β€πŸ’»: That was a lot of information, thank you.
πŸ‘©πŸΌβ€πŸ’»: Welcome!, see you soon.

Why my 0.3 MB image occupies around 10 MB on RAM?
5 (2)

Click to rate this post!
[Total: 2 Average: 5]

You are writing an application that has a long list of entries, with each entry containing an image, the total download size of all images is about 10 MB, but the images take around 200 ~ 300 MB on RAM, you wonder why 🧐?

RAM normally does not understand images that are compressed, they are stored as raw bitmaps, even if the image is compressed, it gets inflated into memory as a raw image.

Image Size on RAM = (pixels height Γ— pixels width Γ— color depth bytes)

The following image takes around 300 KB on disk and has an sRGB color profile, which is 24 bits (8 bits per channel).

Unsplash (CC0)

The size of the image on the RAM would be:

Image Size on RAM = (1665β€Š Γ— β€Š2081 Γ— 3) bytes = 9.8MB

In Swift, Kingfisher comes with an option to downsize images according to the screen scale, so you can have images in a reasonable size even if they come largely from the server.

import UIKit
import Kingfisher

extension UIImageView {
    func setImageAsThumb(url:String) {
        let formattedURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
        let scale = UIScreen.main.scale
        let resizingProcessor = ResizingImageProcessor(referenceSize: CGSize(width: 50.0 * scale, height: 50.0 * scale))
        self.kf.setImage(with: URL(string: formattedURL), placeholder: nil, options: [.processor(resizingProcessor)])


Swift Lexical Structure
5 (2)

Click to rate this post!
[Total: 2 Average: 5]

Swift lexical structure, consists of valid tokens (lowest-level building blocks) that form the structure of any swift program, these tokens describe the rest of whole swift language…

A token consists of an identifier, keyword, punctuation, literal, or operator.

1) Identifiers:
An example of an identifier is a variable name, for example here “pet” is an identifier.

let pet = "Happy Dinosaur πŸ¦–";

Identifiers support unicode characters, you can name you variable in you native language, and as in other programming languages, you cannot use keywords as identifiers, this is still possible if you surrounding a keyword with back-ticks,

var `var` = "var"

examples of unicode identifiers are

var _latitude = 32.0;
var をップル = "apple"

2) Keywords:
The list of basic keywords in swift are listed below, see (Swift Reserved Keywords) for comprehensive list and details.


3) Literals:
literals fall into 3 categories, integer, floating point, and string literals

Integer Literals
var a = 10

var b = 00010100b

var c = 14x

var d = 24o

leading zeros will be ignored by the compiler, and the use of underscores is possible to increase readability.

var a = 100_000_000

Floating Point Literals
//Simple floating point number
var a = 10.7

//Exponent floating point number
var b = 10.6e2

var c = 10.1e-2
//Exponent floating point number

//Hexa decimal exponent
var d = 0xAp2

//Hexa decimal exponent
var d = 0xAp-2

String Literals

String literals are characters are enclosed within double quotes. Strings can contain escape sequences to represent characters like qoutes. Example for string literal is shown below.

var a = “test”
var a = “Hello\nWorld”

\0 Null Character
\ Backslash
\t Horizontal Tab
\n New line
\r Carriage Return
\” Double Quote
\’ Single Quote

4) Operators:
There are different operators supported in swift which includes
+ : Addition
– : Subtraction
* : Multiplication
/ : Division
% : Remainder
^ : Exponent
& : Bitwise And
&& : Logical And
| : Bitwise Or
|| : Logical Or
++ : Increment Operator
– : Minus
~ : Bitwise Not
< : Less Than
> : Greater Than
… etc.

Keep in mind, as in Swift’s official documentation, this is a list of reserved punctuation and can’t be used as custom operators:
(){}[].,:;=@#& (as a prefix operator), ->`?, and ! (as a postfix operator)”

Swift Whitespace:
White spaces are used to separate tokens and to distinguish prefixes, otherwise it’s normally omitted by the compiler.

Swift Comments:
these are statements that are ignored by the compiler, and meant for documentation purposes of our code, they could be either one-line or multi-line.

// This is a single line comment

/* Multi line (block) comment - can have
more than one line! */