28
\$\begingroup\$

Oh no! I'm trapped in a big forest (Okay, not really, but just assume it) and I don't know where I am!

Thankfully, I brought my laptop. But when I searched 'Maps', It said 'No results', and I'm asking this as a last resort.

Please make a program which outputs my location's longitude and latitude in any two number format. any two number format includes:

  • A tuple.
  • A string including the longitude and the latitude.
  • A JSON Object.
  • etc..

Any API or Libraries may be used. That is, You can query Google Maps, etc. (I guess, They didn't trash their APIs :\)

\$\endgroup\$
2
  • 19
    \$\begingroup\$ PowerShell has gl for Get-Location, sadly I don't think C:\Users\Connor will help you escape the forest very much. \$\endgroup\$
    – colsw
    Commented May 5, 2017 at 8:43
  • 1
    \$\begingroup\$ C:\Users\@Connor>sudo yes \$\endgroup\$ Commented May 5, 2017 at 8:47

8 Answers 8

74
\$\begingroup\$

Mathematica, 4 bytes

Err, where am I?

Here

Evaluates to GeoPosition[{latitude, longitude}].

\$\endgroup\$
13
  • 5
    \$\begingroup\$ o_O Mathematica had a builtin for this? \$\endgroup\$ Commented May 5, 2017 at 8:16
  • 4
    \$\begingroup\$ @SIGSEGV Of course... (snippet #4) \$\endgroup\$ Commented May 5, 2017 at 8:17
  • 34
    \$\begingroup\$ Mathematica has a builtin for everything \$\endgroup\$
    – Mayube
    Commented May 5, 2017 at 8:19
  • 10
    \$\begingroup\$ Even better: the command Sunset[Here] - Now correctly returns the amount of time until sunset. \$\endgroup\$ Commented May 5, 2017 at 15:57
  • 4
    \$\begingroup\$ I'm amused to see an answer having this more votes than the question. Congrats 👏👏👏 \$\endgroup\$ Commented May 6, 2017 at 0:30
12
\$\begingroup\$

JavaScript (ES6), 89 82 bytes

navigator.geolocation.watchPosition(x=>alert([(y=x.coords).latitude,y.longitude]))

Try it online! (For some reason, this was not working in the snippet).

Explanation

navigator object is a part of BOM. geolocation is a property of its and is also an object, which contains geolocational data of the user. watchPosition() is a method of geolocation object. It takes in a callback function which is executed after the geolocational data has been retrieved. The callback function x=>alert((y=x.coords).latitude+","+y.longitude) is an anonymous (lambda) function that takes in an object x and alert()s the latitude and longitude properties of x.coords separated by a comma. Note that the object x is passed to the callback function by watchPosition() when the callback function is called (i.e. after the retrieval of the geolocational data).


Note: The geolocational data is GPS-based if GPS is available, otherwise it's based on IP.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ watchPosition instead of getCurrentPosition saves 5 bytes. alert([(y=x.coords).latitude,y.longitude]) saves 2 more \$\endgroup\$ Commented May 5, 2017 at 10:56
  • \$\begingroup\$ 1 character shorter, throws an error: for(f in r=navigator.geolocation)r=r[f](x=>alert((y=x.coords).latitude+","+y.longitude)) \$\endgroup\$ Commented May 5, 2017 at 11:14
  • 3
    \$\begingroup\$ It's a rare occasion indeed when JS can be competitive while relying on its overly long property names. \$\endgroup\$
    – Shaggy
    Commented May 5, 2017 at 20:04
  • \$\begingroup\$ @JohanKarlsson Thanks! Implemented it! \$\endgroup\$
    – Arjun
    Commented May 6, 2017 at 14:41
  • \$\begingroup\$ @Shaggy True, indeed. \$\endgroup\$
    – Arjun
    Commented May 6, 2017 at 14:41
8
\$\begingroup\$

PowerShell, 130 Bytes

Add-Type -A System.Device;($a=[Device.Location.GeoCoordinateWatcher]::new()).Start();for(;($b=$a|% Po*n|% L*)|% I*){}$b|select L*e

Ungolfed:

Add-Type -AssemblyName System.Device #Required to access System.Device.Location namespace
$GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher #Create the required object
$GeoWatcher.Start() #Begin resolving current locaton
whille ($GeoWatcher.Status -ne "Ready") do { sleep -Milliseconds 100 } #Wait for discovery.
$GeoWatcher.Position.Location | Select Latitude,Longitude #Select the relevent results.

Returns as:

PS C:\users\sweeneyc\Desktop> Add-Type -A System.Device;($a=[Device.Location.GeoCoordinateWatcher]::new()).Start();for(;($b=$a|% Po*n|% L*)|% I*){}$b|select L*e

        Latitude         Longitude
        --------         ---------
53.4064177191653 -6.36202495701332

Turns out there is a way to do this, but it's not so pretty.

bonus: opens google maps on your current location.

Add-Type -A System.Device;($a=[Device.Location.GeoCoordinateWatcher]::new()).Start();for(;($b=$a|% Po*n|% L*)|% I*){};saps "https://www.google.ie/maps/@$($b.Latitude),$($b.Longitude)z"

could save 3 bytes using TryStart but it outputs a true to the pipeline and costs more to suppress that.

Add-Type -A System.Device;($a=[Device.Location.GeoCoordinateWatcher]::new()).TryStart($true,[int]9e8);$a|% Po*n|% L*|select L*e

PowerShell (non-competing), 44 Bytes

(irm freegeoip.net/xml).Response|select *ude

Uses an external service since PowerShell has no internal builtins for 'current location' - this is IP-based Geolocation, so almost definitely won't return your actual location, it will return a 'default' location for your ISP, and if you run this over a VPN will present wherever the exit point is.

I've tried (not as a golf exercise) to get google maps API to work with the wireless-mac-address geolocation method, but it's far from accurate or golfable.

marked as non-competing due to the inaccuracy of the results.

PS C:\Users\sweeneyc> (irm freegeoip.net/xml).Response|select *ude
Latitude Longitude
-------- ---------
53.3472  -6.2439

this displays Central Dublin, Ireland when searched up, however i'm a bit outside the city center currently, about 8 miles from the returned point.

\$\endgroup\$
3
  • \$\begingroup\$ In C# the GeoCoordinateWatcher has a TryStart method which you could try which looks like it returns when the status is Ready instead of looping on it. If that method is available obviously. \$\endgroup\$ Commented May 5, 2017 at 13:06
  • 1
    \$\begingroup\$ @TheLethalCoder was the first thing I looked into, added in the example there, it only saves 3 bytes overall, but it dumps a true to the pipeline before I can output the lat/long, and I think it probably violates the spec when that's output. \$\endgroup\$
    – colsw
    Commented May 5, 2017 at 13:19
  • \$\begingroup\$ @TheLethalCoder could feasibly save a byte(s) on the [int]9e8 part when you set the timeout in TryStart but i can't get it to work with 9999999 and [int]9e8 is the same as adding an extra 9. \$\endgroup\$
    – colsw
    Commented May 5, 2017 at 13:21
8
\$\begingroup\$

Python with requests, 101 100 75 bytes

4 bytes saved thanks to @КириллМалышев

21 bytes saved thanks to @Rod

from requests import*;print(get('http://su0.ru/auls').text.split(',')[5:7])
\$\endgroup\$
4
  • \$\begingroup\$ If this is Python 2, you can save 1 byte by removing the parenthesis \$\endgroup\$ Commented May 5, 2017 at 16:16
  • \$\begingroup\$ Maybe it's better to use a short url? For example, su0.ru/auls. So you will save 4 bytes. \$\endgroup\$ Commented May 5, 2017 at 17:36
  • \$\begingroup\$ You don't need all this formatting, from requests import*;print(get('http://ip-api.com/json').text.split(',')[5:7]) should be enough \$\endgroup\$
    – Rod
    Commented May 5, 2017 at 18:17
  • \$\begingroup\$ Link is broken! If you're going to use a URL shortener, best to use an established one. \$\endgroup\$
    – Jakob
    Commented Jun 19, 2018 at 4:40
2
\$\begingroup\$

Angolf, 110 107 bytes

❦™navigator.geolocation.getCurrentPosition#x=>™alert##y=x.coords▷.latitude+","+y.longitude▷▷●
    ❦                                   Define an IIFE
       ™                                Get the scope
         navigator                      Get JavaScript's navigator object from the scope
             geolocation                Get the geolocation property of the navigator object
                 getCurrentPosition     Get the current position and execute a function
                     x=>                Define a function with x as a parameter
                         ™              Get the scope
                            alert       Call alert and alert the following string:
                         #y=x.coords▷ Define y with the value of the coordinates of x
                         latitude       Get y's latitude
                         +","           Append "," to the string
                         +y.longitude   Append the longitude to the string
    ●                                   Close the IIFE and call it with the scope
\$\endgroup\$
2
0
\$\begingroup\$

C#, 132 bytes

_=>{var w=new System.Device.Location.GeoCoordinateWatcher();w.Start();var c=w.Position.Location;return c.Longitude+","+c.Latitude;};
\$\endgroup\$
0
\$\begingroup\$

Excel VBA (Windows, Office 2016+ Only), 278 272 Bytes

Restricted to Windows, Office 2016+ for the use of the Microsoft.Mashup.OleDb.1 provider

Full subroutine that takes no input and outputs your current location as a Excel.ListObject typed table, including extra information.

Sub a
ThisWorkbook.Queries.Add"x","let S=Json.Document(Web.Contents(""su0.ru/auls""))in S
With Sheet1.ListObjects.Add(0,"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=x",,,[A1]).QueryTable
.CommandText="SELECT * FROM [x]
.Refresh
End With
End Sub

Example output excluded for obvious reasons.

-2 Bytes for use of [Sheet1] over Sheets(1)

\$\endgroup\$
0
\$\begingroup\$

Swift 4, 125 117 96 bytes

This can only be tested locally, unfortunately.

import CoreLocation;var c=CLLocationManager().location!.coordinate;print(c.longitude,c.latitude)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.