#' get cities' distance maxtrix
#' @param cities vector
#' @return a distance matrix
#' @export
#' @examples
#' mydat = get_dist_matrix(c("长沙","南昌","郑州","武汉","太原","广州","福州","上海"))
#'
get_dist_matrix <- function(cityname){
library(gmdata)
# get a long and lat data.frame
myda <- gmdata::get_long_lat_Baidu(cityname)
# calculate the distance matrix
nrows <- dim(myda)[1]
print(nrows)
ncols <- dim(myda)[2]
print(ncols)
#library(dplyr)
for (i in 1:(nrows-1)){
colnew <- myda$cities[i]
print(colnew)
myda[colnew] = NA
for (j in (i+1):nrows){
myda[j,i+3] <- gmdata::get_city_dist(myda[i,2],myda[i,3],myda[j,2],myda[j,3])$haversine
}
}
return(myda)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.