1
$\begingroup$

Suppose I'm interested in the latitude of Los Angeles and I do the following:

In[1]:= c1 = CityData[{"LosAngeles", "California", "UnitedStates"}];
In[2]:= Latitude[c1]
Out[2]= Quantity[34.0194, "AngularDegrees"]

So the latitude is a Quantity! And if I get the string value I get:

In[4]:= ToString[Latitude[c1]]
Out[4]= "34.0194 degrees"

This seems a little at odds with the documentation where the examples seem to show a plain number being output.

Quantities are all well and good until I want to, say, use the Google Directions Api:

In[6]:= address = 
 ToString@StringForm[
   "http://maps.googleapis.com/maps/api/directions/json?origin=`1`,`2`", Latitude[c1], Longitude[c1]]
Out[6]= "http://maps.googleapis.com/maps/api/directions/json?origin=34.0194 degrees,-118.411 degrees"

That's not at all what I want!

I suppose I can get the magnitude of the Quantity to get something more like what I want:

In[7]:= address = 
 ToString@StringForm[
   "http://maps.googleapis.com/maps/api/directions/json?origin=`1`,`2`", QuantityMagnitude[Latitude[c1]], QuantityMagnitude[Longitude[c1]]]

Out[7]= "http://maps.googleapis.com/maps/api/directions/json?origin=34.0194,-118.411"

But that's beginning to feel like I'm straining and probably not doing it right. Is there a better way of dealing with these units?

$\endgroup$
  • 1
    $\begingroup$ have you tried Latitude[c1][[1]] and Longitude[c1][[1]]? $\endgroup$ – soandos Jan 1 '15 at 0:58
  • $\begingroup$ The discrepancy between your result and the documentation is probably because those functions didn't return units in previous versions of Mathematica. Re. your question: Try using QuantityMagnitude, for example QuantityMagnitude@Latitude[c1]. $\endgroup$ – C. E. Jan 1 '15 at 1:11
  • $\begingroup$ That produces the right result, of course. Maybe that's the expert's idiom. I'm such a beginner at Mathematica that I'm using more explicit forms so that I know what the code is saying. I was also hoping it was "better code". Even in this form, however, I'm still picking the magnitude out of the Quantity. $\endgroup$ – BigBaaadBob Jan 1 '15 at 1:13
  • $\begingroup$ @Pickett, the change to having Latitude return a Quantity seems like it would have broken existing code that was producing strings from values. That's kinda why I'm wondering if there isn't a backwards-compatibility feature or something. $\endgroup$ – BigBaaadBob Jan 1 '15 at 1:21
  • 3
    $\begingroup$ Using SetSystemOptions["DataOptions" -> "ReturnQuantities" -> False] you can get back the old behaviour (trick from the 'Possible Issues' section of DateDifference). $\endgroup$ – Sjoerd C. de Vries Jan 1 '15 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.