Skip to content

WIP: [RFC]: Bring Flask-GoogleMaps up to date with latest dependencies #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
from flask import Flask, render_template
from flask_googlemaps import GoogleMaps, Map, icons

from flask_googlemaps import GoogleMaps, Map

app = Flask(__name__)
GoogleMaps(app)

pin_content = {
"border_color": "",
"glyph_colors": "",
"background": "",
"glyph": "",
"scale": 2.0,
}
image_content = {
"icon_urls": "https://img.shields.io/badge/PayPal-Donante-red.svg"
}


@app.route("/")
def map_created_in_view():

gmap = Map(
identifier="gmap",
varname="gmap",
lat=37.4419,
lng=-122.1419,
markers={
icons.dots.green: [(37.4419, -122.1419), (37.4500, -122.1350)],
icons.dots.blue: [(37.4300, -122.1400, "Hello World")],
},
markers=[
{
"latitude": 37.4419,
"longitude": -122.1419,
"label": "1",
"content": pin_content,
},
{
"latitude": 37.4519,
"longitude": -122.1519,
"content": image_content,
},
],
style="height:400px;width:600px;margin:0;",
)

Expand Down
5 changes: 3 additions & 2 deletions examples/templates/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ <h2>You can move markers dynamically and add new markers, refresh its position e
<br />
<button onclick='movingmap_markers.map(function(mk){mk.setMap(null)})'>Remove marker</button>
<button onclick='movingmap_markers.map(function(mk){mk.setMap(movingmap)})'>Restore marker</button>
<button onclick='new google.maps.Marker({title: "New Marker", position: {lat: 37.4640, lng: -122.1350}}).setMap(movingmap)'>Add New Marker Above</button>
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker");
<button onclick='new AdvancedMarkerElement({title: "New Marker", position: {lat: 37.4640, lng: -122.1350}}).setMap(movingmap)'>Add New Marker Above</button>


<pre>
Expand All @@ -161,7 +162,7 @@ <h2>You can move markers dynamically and add new markers, refresh its position e
&lt;button onclick='movingmap_markers.map(function(mk){mk.setPosition({lat: 37.449, lng:-122.135})})'&gt;Go to position 4 &lt;/button&gt;
&lt;button onclick='movingmap_markers.map(function(mk){mk.setMap(null)})'&gt;Remove marker&lt;/button&gt;
&lt;button onclick='movingmap_markers.map(function(mk){mk.setMap(movingmap)})'&gt;Restore marker&lt;/button&gt;
&lt;button onclick='onclick='new google.maps.Marker({title: "New Marker", position: {lat: 37.4640, lng: -122.1350}}).setMap(movingmap)''&gt;Add new marker above&lt;/button&gt;
&lt;button onclick='onclick='AdvancedMarkerElement({title: "New Marker", position: {lat: 37.4640, lng: -122.1350}}).setMap(movingmap)''&gt;Add new marker above&lt;/button&gt;
</pre>


Expand Down
75 changes: 8 additions & 67 deletions flask_googlemaps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""FlaskGoogleMaps - Google Maps Extension for Flask"""

__version__ = "0.4.1"
__version__ = "0.5.0"

from json import dumps
from typing import Optional, Dict, Any, List, Union, Tuple, Text # noqa: F401

import requests
from flask import Blueprint, Markup, g, render_template
from flask import Blueprint, g, render_template
from markupsafe import Markup

from flask_googlemaps.icons import dots, Icon # noqa: F401
from flask_googlemaps.marker import Marker

DEFAULT_ICON = dots.red
DEFAULT_CLUSTER_IMAGE_PATH = "static/images/m"


Expand Down Expand Up @@ -66,12 +66,11 @@ def __init__(
self.language = language
self.region = region
self.varname = varname
self.center = self.verify_lat_lng_coordinates(lat, lng)
self.center = Marker.verify_latitude(lat), Marker.verify_longitude(lng)
self.zoom = zoom
self.maptype = maptype
self.markers = [] # type: List[Any]
self.markers = Marker.from_list(markers) # type: List[Marker]
self.map_ids = map_ids
self.build_markers(markers)
self.rectangles = [] # type: List[Any]
self.build_rectangles(rectangles)
self.circles = [] # type: List[Any]
Expand Down Expand Up @@ -108,50 +107,6 @@ def __init__(
self.heatmap_data = []
self.build_heatmap(heatmap_data, heatmap_layer)

def build_markers(self, markers):
# type: (Optional[Union[Dict, List, Tuple]]) -> None
if not markers:
return
if not isinstance(markers, (dict, list, tuple)):
raise AttributeError("markers accepts only dict, list and tuple")

if isinstance(markers, dict):
for icon, marker_list in markers.items():
for marker in marker_list:
marker_dict = self.build_marker_dict(marker, icon=icon)
self.add_marker(**marker_dict)
else:
for marker in markers:
if isinstance(marker, dict):
self.add_marker(**marker)
elif isinstance(marker, (tuple, list)):
marker_dict = self.build_marker_dict(marker)
self.add_marker(**marker_dict)

def build_marker_dict(self, marker, icon=None):
# type: (Union[List, Tuple], Optional[Icon]) -> Dict
marker_dict = {
"lat": marker[0],
"lng": marker[1],
"icon": icon or DEFAULT_ICON,
}
if len(marker) > 2:
marker_dict["infobox"] = marker[2]
if len(marker) > 3:
marker_dict["icon"] = marker[3]
return marker_dict

def add_marker(self, lat=None, lng=None, **kwargs):
# type: (Optional[float], Optional[float], **Any) -> None
if lat is not None:
kwargs["lat"] = lat
if lng is not None:
kwargs["lng"] = lng
if "lat" not in kwargs or "lng" not in kwargs:
raise AttributeError("lat and lng required")

self.markers.append(kwargs)

def build_rectangles(self, rectangles):
# type: (Optional[List[Union[List, Tuple, Tuple[Tuple], Dict]]]) -> None
"""Process data to construct rectangles
Expand Down Expand Up @@ -271,7 +226,7 @@ def build_rectangle_dict(
def add_rectangle(
self, north=None, west=None, south=None, east=None, **kwargs
):
# type: (Optional[float], Optional[float], Optional[float], Optional[float], **Any) -> None # noqa: E501
# type: (Optional[float], Optional[float], Optional[float], Optional[float], **Any) -> None # noqa E501
"""Adds a rectangle dict to the Map.rectangles attribute

The Google Maps API describes a rectangle using the LatLngBounds
Expand Down Expand Up @@ -823,25 +778,11 @@ def as_json(self):

return json_dict

def verify_lat_lng_coordinates(self, lat, lng):
if not (90 >= lat >= -90):
raise AttributeError(
"Latitude must be between -90 and 90 degrees inclusive."
)
if not (180 >= lng >= -180):
raise AttributeError(
"Longitude must be between -180 and 180 degrees inclusive."
)

return (lat, lng)

@property
def js(self):
# type: () -> Markup
return Markup(
self.render(
"googlemaps/gmapjs.html", gmap=self, DEFAULT_ICON=DEFAULT_ICON
)
self.render("googlemaps/gmapjs.html", gmap=self, DEFAULT_PIN="")
)

@property
Expand Down
Loading