Message Boards Message Boards

GeoGraphics shows incorrect locations with Locator and Dynamic

Posted 7 months ago

If we have the following code:

x = {-63., 45}; GeoGraphics[Locator[x]]

This shows the locator in the correct place. But if we add a dynamic, then the locator ends up in the wrong place, and the graph boundaries are huge.

GeoGraphics[Locator[Dynamic[x]]]

Any suggestions?
Thanks

POSTED BY: Vilis Nams
5 Replies

Dynamic[x] is not evaluated by the Kernel while evaluating GeoGraphics[Locator[Dynamic[x]]. It is only after the graphic is returned to the notebook that x is then evaluated. So the original output is "kinda" equivalent to GeoGraphics[Locator[undefined_symbol_here]], which returns the world map plus a pink box for the invalid Locator position.

POSTED BY: Ian Hojnicki

I'm not sure what the precise language design reasons were for the differences in meaning, but I can help clarify how they are different:

In the first case: GeoGraphics[Locator[x]], the kernel does not know that you want to dynamically update the value of x, so, while it updates the coordinates of the Locator, it does not update x.

In the second case GeoGraphics[Locator[Dynamic[x]]], it does update the value of the variable x.

Here is a demonstration to clarify:

DynamicModule[{x1, x2},
 x1 = x2 = {-63.`, 45};
 Grid[{{GeoGraphics[Locator[x1]], Labeled[Dynamic[x1], "x1", Top]},
   {GeoGraphics[Locator[Dynamic[x2]]], 
    Labeled[Dynamic[x2], "x2", Top]}}]
 ]
Posted 7 months ago

Your second and third suggestion worked. Thanks.

I'm puzzled why GeoGraphics[Locator[x]] would be treated differently than GeoGraphics[Locator[Dynamic[x]]].

POSTED BY: Vilis Nams

This seems to work:

Dynamic[GeoGraphics[Locator[GeoPosition[x]], GeoRange -> "World"]]
POSTED BY: Gianluca Gorni

It's unclear what you are trying to do here.

If you are simply trying to display a Locator over a GeoGraphics object, you can do so like this:

LocatorPane[{0, 0}, GeoGraphics["World"]]

If you need the Locator position to be tied to a Dynamic variable, I would suggest something like this:

Manipulate[{LocatorPane[x, GeoGraphics["World"]], x},
 {{x, {0, 0}}, Locator}]

Or alternatively:

DynamicModule[{x = {0, 0}},
 {LocatorPane[Dynamic[x], GeoGraphics["World"]], Dynamic[x]}
 ]

If you're just trying to dynamically update the location on the map, and you don't particularly need the Locator to be there at all, you might be able to use DynamicGeoGraphics, depending on your needs:

DynamicGeoGraphics[GeoPosition[Reverse[{-63., 45}]]]

If you are trying to have the Locator select the location on the map, I would recommend something akin to what many real-time strategy games do, with two maps: one containing the region from which you'd like to sample locations, and one containing the Locator, and the entire region from which you might like to sample. Something like this (where padding is expressed in degrees):

DynamicModule[{x = {-63., 45}, range = Quantity[10, "Kilometers"]}, 
 Column@{LocatorPane[Dynamic[x], GeoGraphics["World"]],
   Dynamic[GeoGraphics[GeoPosition[Reverse[x]],
     GeoRange -> range], SynchronousUpdating -> False]}
 ]

You'll notice that anytime I ask for the GeoPosition of x, I Reverse x first. This is because Locator coordinates are {x,y}, whereas GeoPosition coordinates are {lat,lon}. For north-oriented equirectangular maps, the coordinates are reversed.

I hope this helps!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract