-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.go
42 lines (33 loc) · 914 Bytes
/
location.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package util
import (
"errors"
"strconv"
"strings"
"github.com/TheThingsNetwork/go-account-lib/account"
)
func ParseLocation(locationStr string) (*account.Location, error) {
parts := strings.Split(locationStr, ",")
if len(parts) != 2 {
return nil, errors.New("Location should be on the <latitude>,<longitude> format")
}
lat, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return nil, err
}
if lat < -90 || lat > 90 {
return nil, errors.New("Latitude should be in range [90, 90]")
}
lng, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return nil, err
}
if lng < -180 || lng > 180 {
return nil, errors.New("Longitude should be in range [-180, 180]")
}
return &account.Location{
Latitude: lat,
Longitude: lng,
}, nil
}