gmaps icon indicating copy to clipboard operation
gmaps copied to clipboard

Ability to draw circles on maps

Open pbugnion opened this issue 6 years ago • 6 comments

The drawing layer can currently draw lines and polygons, but it would be useful to also draw circles (cf this tweet).

The GoogleMaps JavaScript API supports circles (see docs).

To implement this, I suggest looking at the current implementation for drawing lines on a map -- see Python side and JavaScript side).

pbugnion avatar Nov 14 '17 18:11 pbugnion

Hi @pbugnion , my workaround to do so was to draw a line from point A to point A.

In order to set colors and width for the strokes and so one, I always draw features through geojson files like so:

def add_point(self, lat, lon, **kwargs):

    # List of kwargs:
        # hover_text = ''
        # fill_color = None
        # fill_opacity = 1.0
        # stroke_color = None
        # stroke_opacity = 1.0
        # scale = 3
        # info_box_content = None
        # display_info_box = None

    # Create Symbol for Point
    # Create json string
    point = geojson.LineString(((lon, lat), (lon, lat)))

    # Create feature
    point_feature = geojson.Feature(geometry=point)

    # Create FeatureCollection
    # gmaps.geojson_layer.InvalidGeoJson: Only FeatureCollection GeoJSON is currently supported
    point_feature_collection = geojson.FeatureCollection([point_feature])

    # Generate JSON string
    json_string = json.loads(geojson.dumps(point_feature_collection, sort_keys=True))

    # Load JSON
    point_json = gmaps.geojson_layer(json_string, **kwargs)




nenetto avatar Jan 28 '18 11:01 nenetto

Thanks very much for submitting a work-around -- it's really valuable!

pbugnion avatar Jan 29 '18 07:01 pbugnion

You're very welcome!

If I have a bit of time I'll try to code it and push it. However, I think my experience on Jupiter widget is not appropriate 🤔

Again, nice job!

El lun., 29 ene. 2018 8:27 a. m., Pascal Bugnion [email protected] escribió:

Thanks very much for submitting a work-around -- it's really valuable!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pbugnion/gmaps/issues/206#issuecomment-361160548, or mute the thread https://github.com/notifications/unsubscribe-auth/ACLa638ycVwNeT_jTSB7P4ECFiK3Yimkks5tPXLugaJpZM4QdylV .

--

Eugenio Marinetto Data Scientist, Strategy Big Data https://www.linkedin.com/in/marinetto/

Email: [email protected] [email protected] http://strategybigdata.es/ https://goo.gl/FnGvfn

nenetto avatar Jan 29 '18 07:01 nenetto

+1

tmiyamon avatar Sep 27 '18 07:09 tmiyamon

I didn't know how to use the given solution.

My workaround was to write a function that, given a lat/lon center of a circle and a radius in meters, create a list of points on the circle and draw lines to connect them.

` def _get_circle(center: List[float], radius: float, num_points = 20) -> List[Tuple[float, float]]:

"""
Inspired by:
https://stackoverflow.com/questions/7477003/calculating-new-longitude-latitude-from-old-n-meters

center: lat/lon of center of circle
radius: radius of circle in m
num_points: how many points of the circle to use

returns: a list of gmaps Lines that approximate a circle
"""

r_earth = 6378 * 1000  # 6378km * 1000m/km

lat = center[LAT]
lon = center[LON]
theta_list = np.arange(0, 360, 360/num_points)

circle_coord_list = []
for theta in theta_list:
    dy = sin(radians(theta)) * radius
    dx = cos(radians(theta)) * radius
    new_latitude = lat + (dy / r_earth) * (180 / pi)
    new_longitude = lon + (dx / r_earth) * (180 / pi) / cos(radians(lat * pi/180))
    circle_coord_list.append((new_latitude, new_longitude))

gmaps_line_list = []
for i, coord in enumerate(circle_coord_list):
    if i == len(circle_coord_list) - 1:  # last coordinate to first coordinate
        gmaps_line_list.append(gmaps.Line(circle_coord_list[i], circle_coord_list[0]))
        break
    gmaps_line_list.append(gmaps.Line(circle_coord_list[i], circle_coord_list[i + 1]))

return gmaps_line_list

`

This can obviously be optimized into one 'for' loop, but I'd figure I'd give users some work to do!

ishan-sinha-keeptruckin avatar Jul 24 '19 18:07 ishan-sinha-keeptruckin

Gmaps supports circles natively via the drawing layer since version 0.8.4. It's just poorly documented.

Read docs on the drawing layer. As well as lines, polygons etc. you can also add instances of gmaps.Circle.

pbugnion avatar Jul 27 '19 07:07 pbugnion