IP to Geolocation map display

I know the geolocation display is cool , but there is one problem , how to get the Geolocation . So I have tried to get Geolocation from Ip , because in my work , I have lots data which are recorded by ip address . I check some R package , find the freegeoip , this is cool !

The work directory and library set up

library(leaflet)
library(maps)
setwd("/Users/eulerwang/R/Products/quiz2")

The freegeoip function

freegeoip <- function(ip, format = ifelse(length(ip)==1,'list','dataframe'))
{
  if (1 == length(ip))
  {
    # a single IP address
    require(rjson)
    url <- paste(c("http://freegeoip.net/json/", ip), collapse='')
    ret <- fromJSON(readLines(url, warn=FALSE))
    if (format == 'dataframe')
      ret <- data.frame(t(unlist(ret)))
    return(ret)
  } else {
    ret <- data.frame()
    for (i in 1:length(ip))
    {
      r <- freegeoip(ip[i], format="dataframe")
      ret <- rbind(ret, r)
    }
    return(ret)
  }
}   

The interactive map setup by ip list

I just try these two ip , I have try my ip , but ,the json query return NA . I think there is some limit in the geolocation’s databse query

The map is zoom out , if you zoom in the map , you will find the ip markers

ip <- c('174.6.153.88')

geolocation <- freegeoip(ip)
## Loading required package: rjson
df <- data.frame(lat = geolocation$latitude,
                 lng = geolocation$longitude)
df %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers(popup = ip )