Here's the code:
library(leaflet)
library(geosphere)
startLoc <- c(-100, 45) #Long/Lat
endLoc <- c(-100, 42) #Long/Lat
totalDist <- distHaversine(startLoc, endLoc)
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=c(startLoc[1],endLoc[1]), lat=c(startLoc[2], endLoc[2]), popup = paste(totalDist)) %>%
addCircles(lng = endLoc[1], lat = endLoc[2], radius = totalDist)
As you can see, the top point is not included in the circle. I think it's because the "Add Circles" doesn't account for the curvature of the earth? Is that correct?
If you use two points closer together, it is better...
This is because Web Mercator is not a distance-preserving projection. Leaflet is drawing a geometric circle on the map, it is not finding points equidistant from the center. The projection stretches distances as you go north, so the northern point is outside the geometric circle. If you try
startLoc <- c(-103, 42) #Long/Lat
endLoc <- c(-100, 42) #Long/Lat
then the left-hand point is on the circle; if you reverse your original points the lower point is inside the circle.
Try this:
library(geosphere)
library(plyr)
startLoc <- c(-100, 45) #Long/Lat
endLoc <- c(-100, 42) #Long/Lat
totalDist <- distHaversine(startLoc, endLoc)
points = ldply(1:360, function(angle) destPoint(endLoc, angle, totalDist))
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=c(startLoc[1],endLoc[1]), lat=c(startLoc[2], endLoc[2]), popup = paste(totalDist)) %>%
addPolygons(lng = points$lon, lat = points$lat)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With