In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge of how leaflet dynamic map, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
Load package:
Library (plyr)
Library (maps)
Library (mapdata)
Library (leaflet)
Library (stringi)
Library (maptools)
Library (htmltools)
Library (RColorBrewer)
Library (ggplot2)
Library (rgdal)
Map material loading: (leaflet supports a variety of map materials, such as common shapefile format, json format and other online maps, but because the json format is not yet very proficient, here use shp format map material as an example to explain).
Some friends will certainly ask, since leaflet itself is an online map, why bother to load shp map material for mapping, because the map base map provided by leaflet online map itself has multi-level scaling characteristics, and each zoom level has corresponding administrative division boundaries, but these parameters and administrative divisions are encapsulated in the underlying js language, and we cannot use R language to call them directly. Can only be used as a layer material for background modification, to put it bluntly, what we want is only multi-level zoom dynamic switching effect, so combined with our custom map material, we can show a more perfect effect. (only thermal filling maps need to define regional boundaries, while point maps and line maps can directly use leaflet's own map material, because only coordinate points are needed.)
There are no more than the following three kinds of map material resources in common use, including map data in R package, shapefile format and json format. The following is the basic code for maps imported in three formats and made in leaflet:
Maps package:
MapStates% addTiles ()% >% addPolygons (fillColor=topo.colors (10Magna alpha = NULL), stroke = FALSE)
MapWorld% addTiles ()% > addPolygons (fillColor = topo.colors (10, alpha = NULL), stroke = FALSE)
Shapefile:
American_map% addTiles ()% >% addPolygons (popup=~STATE_NAME,fillColor=topo.colors (10Magna alpha null), stroke=FALSE)
Json file:
Library (jsonlite)
GeoData%paste (collapse= "\ n")% >% fromJSON (simplifyVector=FALSE)
GeoData$style=list (weight=1,color= "red", fillColor= "green", opacity=1,fill=TRUE,fillOpacity=0.6)
Leaflet () > setView (lng=-98.961387,lat=39.708533,zoom=3) > addTiles () > addGeoJSON (geoData)
The first two kinds of material mapping process is more or less the same, especially some scale attribute declaration is very similar to the ggplot function process, but the json format material operation is not very friendly, his scale attribute is to build style list object for life in the data file, and the json format of list is very much, the structure is relatively complex, so far I do not fully understand if free operation.
The following is the mapping process of the four color scales (three continuity scales and one classification scale) that are displayed in the shapefile format above.
# colorQuantile (percentage color excess of continuity numeric variables):
Pal addTiles () >%
AddPolygons (color=~pal (American_map@data$POP1990), fillOpacity = 0.8 weight 1)% >%
AddLegend (pal=pal,values=American_map@data$POP1990,position= "bottomleft", title = "1990 population (10,000)")
The following two kinds of numerical variables belong to the same continuity (different number of colors are used)
ColorNumeric (Polychromatic excess):
Pal%addTiles ()% >%
AddPolygons (color=~pal (American_map@data$POP1990), fillOpacity = 0.8 weight 1)% >%
AddLegend (pal=pal,values=American_map@data$POP1990,position= "bottomleft", title = "1990 population (ten thousand")
ColorNumeric (monochromatic excess):
Pal%addTiles ()% >%
AddPolygons (color=~pal (American_map@data$POP1990), fillOpacity = 0.8 weight 1)% >%
AddLegend (pal=pal,values=American_map@data$POP1990,position= "bottomleft", title= "1990 population (10,000)")
The following two also belong to the color scale mapping of continuous numerical scalars (but you can customize the number of groups, which actually divides the variables and then fills them in the form of groups, more accurately, it does apply the way of variable conversion (numerical order factor), but the process is automatic, rather than manual conversion as in ggplot).
ColorBin (monochrome gradient overgrouping):
Pal%addTiles ()% >%
AddPolygons (color=~pal (American_map@data$POP1990), fillOpacity=0.8,weight=1)% >%
AddLegend (pal=pal,values=American_map@data$POP1990,position= "bottomleft", title= "1990 population (10,000)")
ColorBin (polychromatic gradient overgrouping):
Pal% addTiles ()% >%
AddPolygons (color=~pal (American_map@data$POP1990), fillOpacity=0.8,weight=1)% >%
AddLegend (pal=pal,values=American_map@data$POP1990,position= "bottomleft", title= "1990 population (10,000)")
Discrete scale: (this example is used in the case where the variable itself is a factor variable, which is a color scale mapping in the case of classified variables)
China_map%
SetView (116.387021) 39.969369)% >%
AddPolygons (fillColor=~pal (shape$type), fillOpacity=0.8,color= "# 000000", weight=1,popup=i_popup)% >%
AddLegend (pal=pal,values=shape@data$type,position= "bottomright", title=paste0 ("Little Rubik's Cube Group 2016")
"," Business Distribution level "))
The above map effects all support multi-section automatic zoom and pop-up window dynamic information display (need to set popup attribute and a small amount of html interaction code)
The bottom layer of leaflet online map is written by js source code, which naturally supports html language. If it can be used in combination with html, you can seamlessly embed many kinds of elements such as pictures, hyperlinks and even videos in dynamic map elements, especially pop-up windows. Interested friends can explore on their own.
The above is all the content of this article "how to dynamic Map of leaflet". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.