dkahle / ggmap

A package for plotting maps in R with ggplot2
763 stars 231 forks source link

Update the markers without refreshing the complete map. #344

Open shahronak47 opened 1 year ago

shahronak47 commented 1 year ago

I have an asset whose position is coming from a database that I am plotting on a map. Every few seconds I want to refresh its position to display its current position. While doing that the complete map gets refreshed showing a small blip for a split second. Is there a way I can avoid that?

Here is a reproducible example below where I am using Sys.sleep(5) to show an update from the database. Ideally, I would expect the point to be updated on the same map itself instead of refreshing the complete map. It becomes really annoying when the map gets updated 10 times in a minute.

library(dplyr)
library(ggmap)

d <- data.frame(lat=c(50.659631, 50.607213, 50.608129),
                lon=c(3.09319, 3.011473, 3.031529))

Lille <- get_map("Lille,France", zoom=12)

p <- ggmap(Lille)
p + geom_point(data=d, aes(x=lon, y=lat), color="red", size=10, alpha=0.5)
Sys.sleep(5)
#shift the points randomly slowly
d1 <- d %>% mutate(lat = lat + runif(n(), -0.01, 0.01), lon = lon + runif(n(), -0.01, 0.01))
p + geom_point(data=d1, aes(x=lon, y=lat), color="red", size=10, alpha=0.5)

I found a few examples on stack overflow but they don't have a concrete answer and none of them has an R answer so I don't know how to implement this.

https://stackoverflow.com/questions/8673111/how-to-update-google-map-pins-without-refreshing-the-whole-map https://stackoverflow.com/questions/43906523/how-to-update-markers-on-google-map-without-refresh https://stackoverflow.com/questions/40491947/how-to-update-location-without-refresh-google-map-using-javascript