A Way to Access Google Streetview from R

A way to access google streetview from R?

My googleway package has a google map widget (and also works with Shiny).

You'll need a valid Google API key to use it

library(googleway)

key <- "your_api_key"

df <- data.frame(lat = -37.817714,
lon = 144.967260,
info = "Flinders Street Station")

google_map(key = key, height = 600, search_box = T) %>%
add_markers(data = df, info_window = "info")

## other available methods
# add_markers
# add_heatmap
# add_circles
# add_traffic
# add_bicycling
# add_transit

Satellite

Sample Image

Satellite / Street

Sample Image

Street view

(notice the marker is still there)

Sample Image


Update - Static Street view map

There's also a google_streetview() function that will download a static streetview image (using the Google Street View Static Image API)

google_streetview(location = c(-37.8177, 144.967),
size = c(400,400),
panorama_id = NULL,
output = "plot",
heading = 90,
fov = 90,
pitch = 0,
response_check = FALSE,
key = key)

Sample Image

Is there an R package to access the google places API?

With ggmap you can get both the lat/lon from an address using geocode(), or the address from a lat/lon using revgeocode().

geocode() is pretty much like searching for a place in google maps. If you don't know the exact address, you can give it a search string and it will try and find it for you.

library(ggmap)

## get lat/lon from address
add <- c("Whitehouse, DC", "Flinders Street Station, Melbourne")

g <- geocode(add)
g
lon lat
1 -77.03673 38.89761
2 144.96706 -37.81827

## get address from lat/lon
## revgeocode(location = c(lon, lat))
apply(g, 1, function(x) revgeocode(location = c(x["lon"], x["lat"])))
[1] "The White House, 1600 Pennsylvania Ave NW, Washington, DC 20500, USA"
[2] "Melbourne VIC 3000, Australia"

Using street map and street view side by side in a xaringan slide

I finally figured it out! Here's what we need to do:

  1. Go to this link to open the Street View Side-By-Side example
  2. Go to All tab and click on the Copy code sample to clipboard menu
  3. Open any text editor (e.g., notepad++), paste the code sample and save the file with .html extension
  4. Go to this link and follow the instructions given under Get the API key section to get an API key
  5. Replace the text YOUR_API_KEY on line 7 of the code sample with the API key obtained from Google through Step 4
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initialize&libraries=&v=weekly"

  1. In the definition of the initialize() function, replace fenway with the desired location name and change the associated lat & lng
  2. Add the following code to xaringan's source Rmd file to create a slide showing the satellite view and street view in a side-by-side fashion
# Adjust width & height to suit your needs
htmltools::tags$iframe(
src = "path/to/street-view-side-by-side.html",
width = 800,
height = 450
)

Google street view URL

Unfortunately not - there's no simple answer, based on the address.

Firstly, the list of parameters for the Google Maps site is documented here, so you can use that as your starting point.

The easy part is that you need to select the streetview layer "&layer=c".

However, before anything will display in that layer, you need to specify where your view is. You set the position by the latitude and longitude in cbll and the angle of the camera with some options in cbp.

To get the latitude and longitude from the address, you need to use a geocoding service, like the google maps api.

However, this will only get you a street view close to the address. In addition to knowing where the street view needs to be from, you also need to know which angle to point the camera at - this will be different for every address, depending on where the nearest point the StreetView camera took a photo from was, so it's not easy to do automatically (with any information that I know is available...)

Automatically open both Google Map and Streetview

Caution

This functionality is currently under development. Progress can be tracked directly on github, but I will endeavour to update this post as I go along.

You can install the development version using

devtools::install_github("SymbolixAU/googleway")

Example

Here's an example of two maps, both controlled by the same street-view 'pegman'.

Note the two UI outputs, the standard map output, and another one I've called pano. This pano is then used in the split_view argument to google_map()

library(shiny)
library(shinydashboard)
library(googleway)

ui <- dashboardPage(

dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(width = 6,
google_mapOutput(outputId = "map")
),
box(width = 6,
google_mapOutput(outputId = "pano")
)
)
)

server <- function(input, output) {
set_key("map_api_key")

output$map <- renderGoogle_map({
google_map(location = c(-37.817386, 144.967463),
zoom = 12,
split_view = "pano")
})
}

shinyApp(ui, server)

Sample Image

Notes

  • if your initial location doesn't fall directly on a valid 'street view' location, the streetview page will be blank

How to save as image response of google streetview API in R?

This is a bit of a guess since I don't have an API key.

This is the last bit of google_streetview function:

map_url <- constructURL(map_url, c(location = location, pano = panorama_id, 
size = size, heading = heading, fov = fov, pitch = pitch,
key = key))
if (output == "plot") {
z <- tempfile()
utils::download.file(map_url, z, mode = "wb")
pic <- jpeg::readJPEG(z)
file.remove(z)
graphics::plot(0:1, 0:1, type = "n", ann = FALSE, axes = FALSE)
return(graphics::rasterImage(pic, 0, 0, 1, 1))
}
else {
return(map_url)
}

Note that if output="plot" then the map_url is downloaded to tempfile, which is read in to a jpeg, which is then plotted, and the temp file removed. How can we get to that jpeg?

If output="html" then the map_url is returned. This must be the URL of the image. So call with output="html" and store the return value, and then download:

url = google_streetview(..., output="html")
t = tempfile()
download.file(url, t, model="wb")
message(t, " should be a JPEG...")

You've tried to do it with output="plot" in which case it returns return(graphics::rasterImage(pic, 0, 0, 1, 1)) which is always a NULL value.

How to detect enter and exit streetView in Google Maps API v3

Observe the visible_changed-event of the streetView, the visible-property will be true or false (open or closed)

      function initialize() {        var mapOptions = {          center: new google.maps.LatLng(52.5498783, 13.425209),          zoom: 8        };        var map = new google.maps.Map(document.getElementById("map-canvas"),            mapOptions);        google.maps.event.addListener(map.getStreetView(),'visible_changed',function(){           alert('streetview is ' +(this.getVisible()?'open':'closed'));        });      }      google.maps.event.addDomListener(window, 'load', initialize);
      html,body,#map-canvas { height: 100%; margin: 0; padding: 0; }
<script src="https://maps.googleapis.com/maps/api/js?v=3&.js"></script><div id="map-canvas"></div>


Related Topics



Leave a reply



Submit