forked from JulianHill/R-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r_foursquare_map.r
112 lines (73 loc) · 2.44 KB
/
r_foursquare_map.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
library(rjson)
library(RCurl)
library(httr)
#Authentication:
require(devtools) #install if necessary (install.packages("devtools")
dev_mode(on=T)
install_github("ThinkToStartR",username="JulianHill")
require(ThinkToStartR)
library(rjson)
require(RCurl)
token <- ThinkToStart("Foursquare_auth",app_name="R_Test",app_id="XXX",app_secret="XXX")
####Get the Data
data <- fromJSON(getURL(paste('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=',token,'&v=',format(Sys.time(), "%Y%m%d"),sep="")))
response <- data$response
venues <- response$venues$items
no_venues = length(data$response$venues$items)
df = data.frame(no = 1:no_venues)
for (i in 1:nrow(df)){
#Add Name and the location of the Venue
df$venue_name[i] <- venues[[i]]$venue$name
df$venue_lat[i] <- venues[[i]]$venue$location$lat
df$venue_lng[i] <- venues[[i]]$venue$location$lng
##########################
#Add the address of the location
if(length(venues[[i]]$venue$location$address)>0)
{
df$venue_address[i] <- venues[[i]]$venue$location$address
}
else{
df$venue_address[i] <- "No Address Available"
}
##########################
#Add the citiy of the location
if(length(venues[[i]]$venue$location$city)>0)
{
df$venue_city[i] <- venues[[i]]$venue$location$city
}
else{
df$venue_city[i] <- "No City Available"
}
##########################
#Add the number of check-ins of the venue
df$venue_checkinsCount[i] <- venues[[i]]$venue$stats[[1]]
##########################
#Add the URL of the URL if defined
if(length(venues[[i]]$venue$url)>0)
{
df$url[i] <- venues[[i]]$venue$url
}
else{
df$url[i] <- NA
}
}
mean_lat <- mean(df$venue_lat) # outcome: 50.90956
mean_lon <- mean(df$venue_lng) # outcome: 7.576119
require(rCharts)
map <- Leaflet$new()
map$setView(c(mean_lat, mean_lon), zoom = 5)
for (i in 1:no_venues){
#Get the name and the number of check-ins of the current venue
name <- df$venue_name[i]
checkins <- df$venue_checkinsCount[i]
#Add the marker to the map but just add a website link if we have a URL for the venue
#if URL is available
if(is.na(df$url[i]))
{ map$marker(c(df$venue_lat[i], df$venue_lng[i]), bindPopup = paste(name,' <br> Checkins: ',checkins,sep=""))
}
else
{
map$marker(c(df$venue_lat[i], df$venue_lng[i]), bindPopup = paste(name,' <br> Checkins: ',checkins,'<br> <a href="',df$url[i],'" target="_blank">Website</a> ',sep=""))
}
}
map