/
address.go
116 lines (100 loc) · 2.88 KB
/
address.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package lorem
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/CiroLee/gear/gearmap"
"github.com/CiroLee/go-lorem/data"
)
type AddressBase struct {
Code string
Name string
}
// return a random Chinese Mainland province name and code
func Province() AddressBase {
var provinces []AddressBase
for _, p := range data.AddressDict {
var item AddressBase = AddressBase{
Code: p.Code,
Name: p.Name,
}
provinces = append(provinces, item)
}
return randomElement(provinces)
}
// return a random Chinese Mainland city name and code
func City() AddressBase {
var province = Province()
return randomCity(province.Code)
}
// return a random Chinese Mainland county name and code
func County() AddressBase {
var province = Province()
var city = randomCity(province.Code)
return randomCounty(province.Code, city.Code)
}
// return a random Chinese Mainland address string that include province, city and county, separated by space
func Address() string {
var province = Province()
var city = randomCity(province.Code)
var county = randomCounty(province.Code, city.Code)
var address = province.Name + " " + city.Name + " " + county.Name
return strings.TrimRight(address, " ")
}
// return a random Chinese Mainland zip code
func ZipCode() int {
var zip, _ = IntBy(6, true)
return zip
}
// return a random longitude and latitude array. support deg format and dms format
func LongAndLat(format string) [2]string {
longitude, _ := randomFloat(-100, 100, 4)
latitude, _ := randomFloat(-90, 90, 4)
if format == "dms" {
return [2]string{degToDms(longitude), degToDms(latitude)}
}
return [2]string{strconv.FormatFloat(longitude, 'f', 4, 64) + "°", strconv.FormatFloat(latitude, 'f', 4, 64) + "°"}
}
func degToDms(num float64) string {
number := math.Abs(num)
deg := math.Floor(number)
minute := math.Floor((number - deg) * 60)
second := ((number-deg)*60 - minute) * 60
second, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", second), 64)
str := strconv.FormatFloat(deg, 'f', 0, 64) + "°" + strconv.FormatFloat(minute, 'f', 0, 64) + "′" + strconv.FormatFloat(second, 'f', 0, 64) + "″"
if num < 0 {
return "-" + str
}
return str
}
func randomCity(provinceCode string) AddressBase {
var _cities []data.Districts = gearmap.Values(data.AddressDict[provinceCode].Cities)
var cities []AddressBase
for _, c := range _cities {
var item AddressBase = AddressBase{
Code: c.Code,
Name: c.Name,
}
cities = append(cities, item)
}
return randomElement(cities)
}
func randomCounty(provinceCode, cityCode string) AddressBase {
var _counties map[string]string = data.AddressDict[provinceCode].Cities[cityCode].Districts
// 部分省市没有区
if len(_counties) == 0 {
return AddressBase{
Code: "",
Name: "",
}
}
var counties []AddressBase
for k, v := range _counties {
counties = append(counties, AddressBase{
Code: k,
Name: v,
})
}
return randomElement(counties)
}