Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
How display popup info in the top right corner?

I was able to display a marker for each part of geography for the USA data (using US data as an example since can't display the work data results) using below:

import dload
from shapely.geometry import shape
import geopandas as gpd
import pandas as pd
import folium

json_string = 'https://raw.githubusercontent.com/datasets/geo-admin1-us/master/data/admin1-us.geojson'

j = dload.json(json_string)

gdf_usa = gpd.GeoDataFrame.from_features(j["features"])
gdf_usa.head()

gdf_usa_new = gpd.GeoDataFrame(gdf_usa, crs="EPSG:4326", geometry='geometry')

usa_map = gdf_usa_new.explore(tiles='CartoDB  positron')
usa_map

gdf_usa_new["long"] = gdf_usa_new.to_crs(epsg='4326').centroid.map(lambda p: p.x)
gdf_usa_new["lat"] = gdf_usa_new.to_crs(epsg='4326').centroid.map(lambda p: p.y)

for i in range(0,len(gdf_usa_new)):
    folium.Marker(
      location=[gdf_usa_new.iloc[i]['lat'], gdf_usa_new.iloc[i]['long']],
      popup=gdf_usa_new.iloc[i]['name'],
      icon=folium.DivIcon(html=f"""<div style="font-family: courier new; color: white">{gdf_usa_new.iloc[i]['name']}</div>""")
   ).add_to(usa_map)

usa_map

As can be seen in the map below, the popup appears right where I hover the mouse:

enter image description here

How can I modify the code so that when I hover over the desired area the information is displayed in the top right corner like below?

enter image description here

Above screenshot taken from:

https://leafletjs.com/examples/choropleth/

So, I added the code below. However, it does not provide the desired update.

my_js = '''
var info = L.control({position: 'topright'});

info.onAdd = function (usa_map) {
    this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
    this.update();
    return this._div;
};

// method that we will use to update the control based on feature properties passed
info.update = function (props) {
    this._div.innerHTML = '<h4>US Population Density</h4>' +  (gdf_usa_new ?
        '<b>' + gdf_usa_new + '</b><br />'
        : 'Hover over a state');
};'''
usa_map.get_root().script.add_child(Element(my_js))

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Solved it. Follow the link (https://leafletjs.com/examples/choropleth/). Copy below into the html code (drag the html into the Notepad++). Update "map" to "map_...", just search for "map_" in the text file that opened.

var info = L.control();

info.onAdd = function (map) {
    this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
    this.update();
    return this._div;
};

// method that we will use to update the control based on feature properties passed
info.update = function (props) {
    this._div.innerHTML = '<h4>US Population Density</h4>' +  (props ?
        '<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
        : 'Hover over a state');
};

info.addTo(map);

Next, search for "highlight" in the txt file of your html. Add below right under the function highlighter(feature) or whatever it is called in your file:

info.update(layer.feature.properties);

Save the notepad++, open your html file, and it should work!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda