1313def location_ui (
1414 label : str = "Location" ,
1515 * ,
16- lat : Optional [float ],
17- long : Optional [float ],
16+ lat : Optional [float ] = None ,
17+ long : Optional [float ] = None ,
1818) -> ui .TagChildArg :
1919 return ui .div (
20- ui .input_numeric ("lat" , "Latitude" , value = None ),
21- ui .input_numeric ("long" , "Longitude" , value = None ),
20+ ui .input_numeric ("lat" , "Latitude" , value = lat ),
21+ ui .input_numeric ("long" , "Longitude" , value = long ),
2222 ui .help_text ("Click to select location" ),
2323 output_widget ("map" , height = "200px" ),
2424 ui .tags .style (
@@ -39,9 +39,12 @@ def location_server(
3939 marker = L .Marker (location = (52 , 100 ))
4040
4141 @reactive .Effect
42- @reactive .isolate ()
42+ @reactive .isolate () # Use this to ensure we only execute one time
4343 def _ ():
44- if not input .lat () and not input .long ():
44+ if input .lat () is None and input .long () is None :
45+ ui .notification_show (
46+ "Searching for location..." , duration = False , id = "searching"
47+ )
4548 ui .insert_ui (
4649 ui .tags .script (
4750 """
@@ -51,7 +54,7 @@ def _():
5154 Shiny.setInputValue("#HERE#", {latitude, longitude});
5255 },
5356 (err) => {
54- Shiny.setInputValue("#HERE#", null );
57+ Shiny.setInputValue("#HERE#", {latitude: 0, longitude: 0} );
5558 },
5659 {maximumAge: Infinity, timeout: Infinity}
5760 )
@@ -90,6 +93,7 @@ def on_marker_move():
9093 @reactive .Effect
9194 def detect_location ():
9295 coords = input .here ()
96+ ui .notification_remove ("searching" )
9397 if coords and not input .lat () and not input .long ():
9498 ui .update_numeric ("lat" , value = coords ["latitude" ])
9599 ui .update_numeric ("long" , value = coords ["longitude" ])
@@ -116,11 +120,17 @@ def sync_map_long():
116120
117121 @reactive .Calc
118122 def location ():
123+ """Returns tuple of (lat,long) floats--or throws silent error if no lat/long is
124+ selected"""
125+
126+ # Require lat/long to be populated before we can proceed
119127 req (input .lat () is not None , input .long () is not None )
120- long = float (input .long ())
121- if wrap_long :
122- long = (long + 180 ) % 360 - 180
128+
123129 try :
130+ long = float (input .long ())
131+ # Wrap longitudes so they're within [-180, 180]
132+ if wrap_long :
133+ long = (long + 180 ) % 360 - 180
124134 return (float (input .lat ()), long )
125135 except ValueError :
126136 raise ValueError ("Invalid latitude/longitude specification" )
0 commit comments