-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"github.com/jasonmoo/geo" | ||
) | ||
|
||
// Use the name of the command to figure out if we want to do a straight or a | ||
// reverse lookup to the geocoding API. Anything with reverse in its name will | ||
// be doing a reverse lookup. | ||
func isReverseMode() bool { | ||
commandName := os.Args[0] | ||
reverseMatcher := regexp.MustCompile("reverse") | ||
return reverseMatcher.MatchString(commandName) | ||
} | ||
|
||
func parseCoordinates() (latitude, longitude float64, ok bool) { | ||
latitude, longitude, ok = 0, 0, false | ||
|
||
// Assuming seperated coordinates | ||
if len(os.Args) == 3 { | ||
lat, latOk := strconv.ParseFloat(os.Args[1], 64) | ||
long, longOk := strconv.ParseFloat(os.Args[2], 64) | ||
|
||
if nil != latOk || nil != longOk { | ||
return | ||
} | ||
|
||
latitude, longitude, ok = lat, long, true | ||
} else if len(os.Args) == 2 { | ||
latitude = 0 | ||
longitude = 1 | ||
ok = true | ||
} | ||
|
||
return | ||
} | ||
|
||
func ReverseGeocode(latitude, longitude float64) (*geo.Address, error) { | ||
coordinateString := fmt.Sprintf("%v,%v", latitude, longitude) | ||
return geo.ReverseGeocode(coordinateString) | ||
} | ||
|
||
func main() { | ||
var address *geo.Address | ||
var error error | ||
|
||
if isReverseMode() { | ||
latitude, longitude, ok := parseCoordinates() | ||
|
||
if ok != false { | ||
address, error = ReverseGeocode(latitude, longitude) | ||
} | ||
} else { | ||
query := strings.Join(os.Args[1:], " ") | ||
address, error = geo.Geocode(query) | ||
} | ||
|
||
if nil != error { | ||
fmt.Fprintln(os.Stderr, "Did not get any result") | ||
os.Exit(0) | ||
} | ||
|
||
fmt.Println(address) | ||
} |