-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoding_R_example.Rmd
More file actions
70 lines (55 loc) · 1.41 KB
/
geocoding_R_example.Rmd
File metadata and controls
70 lines (55 loc) · 1.41 KB
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
---
title: "Google Geocoding R Example"
author: "Sophia Luo"
date: "2024-10-02"
output: html_document
---
```{r setup, include=FALSE}
# install.packages("mapsapi")
library(mapsapi)
#### NOTE that this is Sophia's personal API key. Don't share it! ####
key = "redacted"
##############
```
Load sample data
```{r}
# install.packages("readxl")
library(readxl)
data <- readxl::read_excel("/Users/luoda/Downloads/StrayReport09.04.24.xlsx")
data$`Location Found`
data$LocationPlus <- paste(data$`Location Found`, data$`Jurisdiction In`, ", Michigan")
```
Find latitude and longitude of sample data
```{r}
# No results found for many addresses when you only use Location Found
# testGeocode = mp_geocode(
# addresses = data$`Location Found`,
# key = "AIzaSyCQhSlO7I12W_LENQsmU_JMkT7hFa4zmRw",
# quiet = TRUE
# )
#
# testGeocode # An XML document
testGeocode2 = mp_geocode(
addresses = data$LocationPlus,
key = "AIzaSyCQhSlO7I12W_LENQsmU_JMkT7hFa4zmRw",
quiet = TRUE
)
testGeocode2
```
Convert XML output to geocoded point locations
```{r}
# points = mp_get_points(testGeocode)
# points$pnt
points2 = mp_get_points(testGeocode2)
```
Use leaflet for plotting points on a map
```{r}
# install.packages("leaflet")
library(leaflet)
leaflet() %>%
addProviderTiles("CartoDB.DarkMatter") %>%
addCircleMarkers(data = points)
leaflet() %>%
addProviderTiles("CartoDB.DarkMatter") %>%
addCircleMarkers(data = points2)
```