如何从用户输入中更新标记并将其添加到Python Folium地图?

时间:2019-06-11 19:57:06

标签: python mongodb pandas folium geopy

我想知道您是否可以解决我遇到的一个小问题。我在一页上制作了一个计划板,人们可以在上面输入国家/地区数据,然后在另一页上创建了一张地图,在他们选择的位置上绘制了一个标记,并且一切正常,但是如果我编辑一个在停止运行脚本之前不会更新地图。请给我一些指示吗?

我用大叶草创建了它,并使用了熊猫,MongoDB和geopy。

我已经尝试过,而同时又循环了

我正在考虑在结尾处添加else语句?

任何建议将不胜感激

import os
from flask import Flask, render_template, url_for, request, redirect
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
import folium
import requests
import pandas as pd
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter

app = Flask(__name__)
geolocator = Nominatim(user_agent="Travel Tracker")
app.config["MONGO_DBNAME"] = 'travel_tracker'
app.config["MONGO_URI"] = os.getenv('MONGO_URI')

mongo = PyMongo(app)
countries = mongo.db.country.find()
hotels = mongo.db.hotel.find()
map_obj = folium.Map(location=[53.49, -2.24], zoom_start=5)
#map_obj.save('templates/travelmap.html')
url = requests.get('https://ipinfo.io/')

@app.route('/my_map')
def my_map():
    global map_obj
    return map_obj.get_root().render()

# Tracks your current location
@app.route('/travel_map')
def travel_map():
    global map_obj   
    data = url.json()
    my_loc = data['loc'].split(',')
    folium.Marker(location=my_loc, popup="I am here").add_to(map_obj)

# Pulls info from MongoDB to get the coordinates to display on map
    fg = folium.FeatureGroup(name="travel_map")
    specific_country = mongo.db.country.find_one()
    for count in countries:
        df = pd.DataFrame({'name': count['country_name']}, index=[0])
        geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
        df['location'] = df['name'].apply(geocode)
        df['point'] = df['location'].apply(lambda loc: tuple(loc.point) if loc else None)
        fg.add_child(folium.Marker(location=df['point'][0][:-1], popup=count['country_name'], icon=folium.Icon(color='purple')))
        map_obj.add_child(fg)    

    return render_template('map.html', specific_country=specific_country)

0 个答案:

没有答案