2
2
3
3
__version__ = "0.4.0"
4
4
5
- from flask import render_template , Blueprint , Markup , g
6
- from flask_googlemaps .icons import dots
7
5
from json import dumps
8
6
7
+ import requests
8
+ from flask import Blueprint , Markup , g , render_template
9
+
10
+ from flask_googlemaps .icons import dots
11
+
9
12
DEFAULT_ICON = dots .red
10
13
DEFAULT_CLUSTER_IMAGE_PATH = "static/images/m"
11
14
@@ -227,19 +230,12 @@ def build_rectangle_dict(
227
230
"stroke_weight" : stroke_weight ,
228
231
"fill_color" : fill_color ,
229
232
"fill_opacity" : fill_opacity ,
230
- "bounds" : {
231
- "north" : north ,
232
- "west" : west ,
233
- "south" : south ,
234
- "east" : east ,
235
- },
233
+ "bounds" : {"north" : north , "west" : west , "south" : south , "east" : east }
236
234
}
237
235
238
236
return rectangle
239
237
240
- def add_rectangle (
241
- self , north = None , west = None , south = None , east = None , ** kwargs
242
- ):
238
+ def add_rectangle (self , north = None , west = None , south = None , east = None , ** kwargs ):
243
239
""" Adds a rectangle dict to the Map.rectangles attribute
244
240
245
241
The Google Maps API describes a rectangle using the LatLngBounds
@@ -274,9 +270,7 @@ def add_rectangle(
274
270
if east :
275
271
kwargs ["bounds" ]["east" ] = east
276
272
277
- if set (("north" , "east" , "south" , "west" )) != set (
278
- kwargs ["bounds" ].keys ()
279
- ):
273
+ if set (("north" , "east" , "south" , "west" )) != set (kwargs ["bounds" ].keys ()):
280
274
raise AttributeError ("rectangle bounds required to rectangles" )
281
275
282
276
kwargs .setdefault ("stroke_color" , "#FF0000" )
@@ -324,9 +318,7 @@ def build_circles(self, circles):
324
318
elif isinstance (circle , (tuple , list )):
325
319
if len (circle ) != 3 :
326
320
raise AttributeError ("circle requires center and radius" )
327
- circle_dict = self .build_circle_dict (
328
- circle [0 ], circle [1 ], circle [2 ]
329
- )
321
+ circle_dict = self .build_circle_dict (circle [0 ], circle [1 ], circle [2 ])
330
322
self .add_circle (** circle_dict )
331
323
332
324
def build_circle_dict (
@@ -372,9 +364,7 @@ def build_circle_dict(
372
364
373
365
return circle
374
366
375
- def add_circle (
376
- self , center_lat = None , center_lng = None , radius = None , ** kwargs
377
- ):
367
+ def add_circle (self , center_lat = None , center_lng = None , radius = None , ** kwargs ):
378
368
""" Adds a circle dict to the Map.circles attribute
379
369
380
370
The circle in a sphere is called "spherical cap" and is defined in the
@@ -746,9 +736,7 @@ def as_json(self):
746
736
@property
747
737
def js (self ):
748
738
return Markup (
749
- self .render (
750
- "googlemaps/gmapjs.html" , gmap = self , DEFAULT_ICON = DEFAULT_ICON
751
- )
739
+ self .render ("googlemaps/gmapjs.html" , gmap = self , DEFAULT_ICON = DEFAULT_ICON )
752
740
)
753
741
754
742
@property
@@ -778,24 +766,33 @@ def set_googlemaps_loaded():
778
766
g .googlemaps_loaded = True
779
767
return ""
780
768
781
- def get_address (API_KEY ,lat ,lon ):
769
+
770
+ def get_address (API_KEY , lat , lon ):
782
771
add_dict = dict ()
783
- response = rq .get ('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + ',' .join (map (str ,[lat ,lon ]))+ '&key=' + API_KEY ).json ()
784
- add_dict ['zip' ] = response ['results' ][0 ]['address_components' ][- 1 ]['long_name' ]
785
- add_dict ['country' ] = response ['results' ][0 ]['address_components' ][- 2 ]['long_name' ]
786
- add_dict ['state' ] = response ['results' ][0 ]['address_components' ][- 3 ]['long_name' ]
787
- add_dict ['city' ] = response ['results' ][0 ]['address_components' ][- 4 ]['long_name' ]
788
- add_dict ['locality' ] = response ['results' ][0 ]['address_components' ][- 5 ]['long_name' ]
789
- add_dict ['road' ] = response ['results' ][0 ]['address_components' ][- 6 ]['long_name' ]
790
- add_dict ['formatted_address' ] = response ['results' ][0 ]['formatted_address' ]
772
+ response = requests .get (
773
+ "https://maps.googleapis.com/maps/api/geocode/json?latlng="
774
+ + "," .join (map (str , [lat , lon ]))
775
+ + "&key="
776
+ + API_KEY
777
+ ).json ()
778
+ add_dict ["zip" ] = response ["results" ][0 ]["address_components" ][- 1 ]["long_name" ]
779
+ add_dict ["country" ] = response ["results" ][0 ]["address_components" ][- 2 ]["long_name" ]
780
+ add_dict ["state" ] = response ["results" ][0 ]["address_components" ][- 3 ]["long_name" ]
781
+ add_dict ["city" ] = response ["results" ][0 ]["address_components" ][- 4 ]["long_name" ]
782
+ add_dict ["locality" ] = response ["results" ][0 ]["address_components" ][- 5 ]["long_name" ]
783
+ add_dict ["road" ] = response ["results" ][0 ]["address_components" ][- 6 ]["long_name" ]
784
+ add_dict ["formatted_address" ] = response ["results" ][0 ]["formatted_address" ]
791
785
return add_dict
792
-
793
-
794
-
795
- def get_coordinates ( API_KEY ,address_text ):
796
- response = rq .get ('https://maps.googleapis.com/maps/api/geocode/json?address=' + address_text + '&key=' + API_KEY ).json ()
797
- return response ['results' ][0 ]['geometry' ]['location' ]
798
-
786
+
787
+
788
+ def get_coordinates (API_KEY , address_text ):
789
+ response = requests .get (
790
+ "https://maps.googleapis.com/maps/api/geocode/json?address="
791
+ + address_text
792
+ + "&key="
793
+ + API_KEY
794
+ ).json ()
795
+ return response ["results" ][0 ]["geometry" ]["location" ]
799
796
800
797
801
798
def is_googlemaps_loaded ():
@@ -817,9 +814,7 @@ def init_app(self, app):
817
814
app .add_template_global (googlemap_obj )
818
815
app .add_template_filter (googlemap )
819
816
app .add_template_global (googlemap )
820
- app .add_template_global (
821
- app .config .get ("GOOGLEMAPS_KEY" ), name = "GOOGLEMAPS_KEY"
822
- )
817
+ app .add_template_global (app .config .get ("GOOGLEMAPS_KEY" ), name = "GOOGLEMAPS_KEY" )
823
818
app .add_template_global (set_googlemaps_loaded )
824
819
app .add_template_global (is_googlemaps_loaded )
825
820
0 commit comments