26
\$\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\$
  • 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 May 5 '17 at 8:43
  • \$\begingroup\$ C:\Users\@Connor>sudo yes \$\endgroup\$ – Matthew Roh May 5 '17 at 8:47
72
\$\begingroup\$

Mathematica, 4 bytes

Err, where am I?

Here

Evaluates to GeoPosition[{latitude, longitude}].

\$\endgroup\$
  • 5
    \$\begingroup\$ o_O Mathematica had a builtin for this? \$\endgroup\$ – Matthew Roh May 5 '17 at 8:16
  • 4
    \$\begingroup\$ @SIGSEGV Of course... (snippet #4) \$\endgroup\$ – Martin Ender May 5 '17 at 8:17
  • 33
    \$\begingroup\$ Mathematica has a builtin for everything \$\endgroup\$ – Skidsdev May 5 '17 at 8:19
  • 10
    \$\begingroup\$ Even better: the command Sunset[Here] - Now correctly returns the amount of time until sunset. \$\endgroup\$ – Greg Martin May 5 '17 at 15:57
  • 4
    \$\begingroup\$ I'm amused to see an answer having this more votes than the question. Congrats 👏👏👏 \$\endgroup\$ – Matthew Roh May 6 '17 at 0:30
11
\$\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\$
  • 2
    \$\begingroup\$ watchPosition instead of getCurrentPosition saves 5 bytes. alert([(y=x.coords).latitude,y.longitude]) saves 2 more \$\endgroup\$ – Johan Karlsson May 5 '17 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\$ – user2428118 May 5 '17 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 May 5 '17 at 20:04
  • \$\begingroup\$ @JohanKarlsson Thanks! Implemented it! \$\endgroup\$ – Arjun May 6 '17 at 14:41
  • \$\begingroup\$ @Shaggy True, indeed. \$\endgroup\$ – Arjun May 6 '17 at 14:41
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\$
  • \$\begingroup\$ If this is Python 2, you can save 1 byte by removing the parenthesis \$\endgroup\$ – Wondercricket May 5 '17 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\$ – Кирилл Малышев May 5 '17 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 May 5 '17 at 18:17
  • \$\begingroup\$ Link is broken! If you're going to use a URL shortener, best to use an established one. \$\endgroup\$ – Jakob Jun 19 '18 at 4:40
5
\$\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\$
  • \$\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\$ – TheLethalCoder May 5 '17 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 May 5 '17 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 May 5 '17 at 13:21
2
\$\begingroup\$

(non-competing) 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\$
  • 1
    \$\begingroup\$ Welcome to the site! I can't seem to find any information about Angolf; would you mind linking its website? \$\endgroup\$ – ETHproductions May 6 '17 at 0:11
  • \$\begingroup\$ github.com/XtremePlayzCODE/angled/blob/master/angolf.js, Sorry for the late reply \$\endgroup\$ – thatoneuser May 6 '17 at 0:37
  • 1
    \$\begingroup\$ Welcome to the site! do you have an older link than this? if not, the language is considered to be newer than the challenge. it is the custom to mark answers in languages newer than the challenge with (non-competing) in the title \$\endgroup\$ – Destructible Lemon May 6 '17 at 0:56
  • \$\begingroup\$ I've had Angolf for 3 months now but I just now uploaded it to GitHub as it didn't have a link before \$\endgroup\$ – thatoneuser May 6 '17 at 1:01
  • \$\begingroup\$ @thatoneuser unfortunately on this sight languages need to have been published to be marked as "competing" please do mark it as noncompeting (this means you may participate and garner upvotes, but you will cont be considered for an accepted tic mark. \$\endgroup\$ – Rohan Jhunjhunwala May 6 '17 at 1:22
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, privacy policy and cookie policy

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