<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/the-meeting-point-locator/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fronkonstin.com/2017/01/16/the-meeting-point-locator/"> R – Fronkonstin</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="twitter-share"><a target="_blank" class="twitter-share-button" rel="nofollow" href="https://twitter.com/intent/tweet?url=https%3A%2F%2Ffronkonstin.com%2F2017%2F01%2F16%2Fthe-meeting-point-locator%2F&via=aschinchon">Tweet</a></div>
<blockquote><p>Hi Hillary, It’s Donald, would you like to have a beer with me in La Cabra Brewing, in Berwyn, Pensilvania? (Hypothetical utilization of The Meeting Point Locator)</p>
</blockquote>
<p>Finding a place to have a drink with someone may become a difficult task. It is quite common that one of them does not want to move to the other’s territory. I am sure you have faced to this situation many times. With <em>The Meeting Point Locator</em> this will be no longer an issue, since it will give you a list of equidistant bars and coffees to any two given locations. Let’s see an example.</p>
<p>I do not know if Hillary Clinton and Donald Trump have met each other after the recent elections in United States, but the will probably do. Let’s suppose Hillary doesn’t want to go to The White House and that Donald prefers another place instead Hillary’s home. No problem at all. According to <a target="_blank" rel="nofollow" href="http://www.oprah.com/oprahshow/tour-bill-and-hillary-clintons-chappaqua-new-york-home-video">this</a>, Hillary lives in <strong>Chappaqua, New York </strong>and Donald will live in <strong>The White House, Washington </strong>(although he supposedly won’t do full time as he <a target="_blank" rel="nofollow" href="http://occupydemocrats.com/2016/11/12/trump-just-announced-wont-live-white-house-full-time-wants-golf-relax/">announced recently</a>). These two locations are the only input that <em>The Meeting Point Locator</em> needs to purpose equidistant places where having a drink. This is how it works:</p>
<ul>
<li>Generates a number of coordinates on the great circle which passes through the midpoint of the original locations and is orthogonal to the rhumb defined by them; the number of points depends on the distance between the original locations.</li>
<li>Arranges these coordinates according to the distance to the original locations, from the nearest to the most distant.</li>
<li>Depending also on the distance of the original locations, defines a radius to search around each point generated on the great circle (once calculated, this radius is constant for all searches).</li>
<li>Starting from the nearest point, looks for a number of places (20 by default) to have a drink using the radius calculated previously. To do this, it calls to the <a target="_blank" rel="nofollow" href="https://developers.google.com/places/web-service/?hl=en-419">Google Places API</a>. Once the number of locations is reached, the proccess stops.</li>
</ul>
<p>This map shows the places purposed for Hillary and Donald (blue points) as well as the original locations (red ones). You can make <em>zoom in</em> for details:</p>
<p><br>
<iframe height="500" width="100%" src="https://fronkonstin.com/wp-content/uploads/2017/01/HillaryDonaldMap.html"></iframe></p>
<p>These are the 20 closest places to both of them:</p>
<p><img sizes="(max-width: 604px) 100vw, 604px" alt="listHillaryTrumpDT" class="wp-image-3827" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/01/listHillaryTrumpDT.png?resize=604%2C451&ssl=1" data-recalc-dims="1"></p>
<p>Some other examples of the <em>utility</em> of <em>The Meeting Point Locator</em>:</p>
<ul>
<li><strong>Pau Gasol</strong> (who lives in San Antonio, Texas) and <strong>Marc Gasol</strong> (in Memphis, Tennessee) can meet in <strong><em>The Draft Sports Bar, </em>in Leesville (Louisiana)</strong> to have a beer while watching a NBA match. It is 537 kilometers far from both of them.</li>
<li><strong>Bob Dylan</strong> (who lives in Malibu, California) and <strong>The Swedish Academy</strong> (placed in Stockholm, Sweden) can <em>smooth things over</em> drinking a caipirinha in <strong><em>Bar São João</em>, in Tremedal (Brasil)</strong>, <em>only</em> 9.810 kilometers far from both of them.</li>
<li><strong>Spiderman</strong> (placed in New York City) and <strong>Doraemon</strong> (in Tokio, Japan) can meet in <strong>Andreyevskaya, in Stroitel (Russia)</strong> to have a have a hot drink. Since they are <em>superheroes</em>, they will cover the 9.810 kilometers of separation in no time at all.</li>
</ul>
<p>I faced with two challenges to do this experiment: how to generate the orthogonal great circle from two given locations and how to define radius and number of points over this circle to do searchings. I will try to explain in depth both things in the future in another post.</p>
<p>You will find the code below. To make it work, do not forget to get your own key for Google Places API Web Service <a target="_blank" rel="nofollow" href="https://developers.google.com/places/web-service/get-api-key">here</a>. I hope this tool will be helpful for someone; if yes, do not hesitate to tell it to me.</p>
<pre>
library(httr)
library(jsonlite)
library(dplyr)
library(ggmap)
library(geosphere)
library(DT)
library(leaflet)

# Write both addresses here (input)
place1=&quot;Chappaqua, New York, United States of America&quot;
place2=&quot;The White House, Washington DC, United States of America&quot;

# Call to Google Maps API to obtain coordinates of previous addresses
p1=geocode(place1, output = &quot;latlon&quot;)
p2=geocode(place2, output = &quot;latlon&quot;)

# To do searchings I need a radius
radius=ifelse(distGeo(p1, p2)>1000000, 10000,
              ifelse(distGeo(p1, p2)>100000, 2500, 1000))

# And a number of points
npoints=ifelse(distGeo(p1, p2)>1000000, 2002,
               ifelse(distGeo(p1, p2)>100000, 7991, 19744))

# Place here the Google Places API Key
key=&quot;PLACE_YOUR_OWN_KEY_HERE&quot;

# Build the url to look for bars and cafes with the previous key
url1=&quot;https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=lat,lon&amp;radius=&quot;
url2=&quot;&amp;types=cafe|bar&amp;key=&quot;
url=paste0(url1,radius,url2,key)

# This is to obtain the great circle orthogonal to the rhumb defined by input locations
# and which passes over the midpoint. I will explain this step in the future
mid=midPoint(p1, p2)
dist=distGeo(p1, p2)
x=p1
y=p2
while(dist>1000000)
{
  x=midPoint(mid, x)
  y=midPoint(mid, y)
  dist=distGeo(x, y)
}

bea=bearingRhumb(x, y)
points=greatCircle(destPoint(p=mid, b=bea+90, d=1), mid, n=npoints)

# Arrange the points dependning on the distance to the input locations
data.frame(dist2p1=apply(points, 1, function (x) distGeo(p1, x)),
           dist2p2=apply(points, 1, function (x) distGeo(p2, x))) %>% 
  mutate(order=apply(., 1, function(x) {max(x)})) %>% 
  cbind(points) %>% 
  arrange(order) -> points

# Start searchings
nlocs=0 # locations counter (by default stops when 20 is reached)
niter=1 # iterations counter (if greater than number of points on the great circle, stops)
results=data.frame()
while(!(nlocs>=20 | niter>npoints)) {
  print(niter)
  url %>% 
    gsub(&quot;lat&quot;, points[niter, 'lat'], .) %>% 
    gsub(&quot;lon&quot;, points[niter, 'lon'], .) %>% 
    GET %>% 
    content(&quot;text&quot;) %>% 
    fromJSON -> retrieve
  
  df=data.frame(lat=retrieve$results$geometry$location$lat,
                lng=retrieve$results$geometry$location$lng,
                name=retrieve$results$name, 
                address=retrieve$results$vicinity)
  results %>% rbind(df)->results
  
  nlocs=nlocs+nrow(df)
  niter=niter+1 
}

# I prepare results to do a Data Table
data.frame(dist2p1=apply(results, 1, function (x) round(distGeo(p1, c(as.numeric(x[2]), as.numeric(x[1])))/1000, digits=1)),
           dist2p2=apply(results, 1, function (x) round(distGeo(p2, c(as.numeric(x[2]), as.numeric(x[1])))/1000, digits=1))) %>% 
  mutate(mx=apply(., 1, function(x) {max(x)})) %>% 
  cbind(results) %>% 
  arrange(mx) %>% 
  mutate(rank=row_number()) %>% 
  select(-mx)-> resultsDT

# This is the Data table
datatable(resultsDT, 
          class = 'cell-border stripe',
          rownames = FALSE,
          options = list(pageLength = 5),
          colnames = c('Distance to A (Km)', 
                       'Distance to B (Km)', 
                       'Latitude', 
                       'Longitude',
                       'Name', 
                       'Address', 
                       'Rank'))

# Map with the locations using leaflet
resultsDT %>% 
  leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(
    lng=resultsDT$lng, lat=resultsDT$lat,
    radius = 8,
    color = &quot;blue&quot;,
    stroke = FALSE, fillOpacity = 0.5,
    popup=paste(paste0(&quot;&lt;b>&quot;, resultsDT$name, &quot;&lt;/b>&quot;), resultsDT$address, sep=&quot;
&quot;)
  ) %>% 
  addCircleMarkers(
    lng=p1$lon, lat=p1$lat,
    radius = 10,
    color = &quot;red&quot;,
    stroke = FALSE, fillOpacity = 0.5,
    popup=paste(&quot;&lt;b>Place 1&lt;/b>&quot;, place1, sep=&quot;
&quot;)
  )%>% 
  addCircleMarkers(
    lng=p2$lon, lat=p2$lat,
    radius = 10,
    color = &quot;red&quot;,
    stroke = FALSE, fillOpacity = 0.5,
    popup=paste(&quot;&lt;b>Place 2&lt;/b>&quot;, place2, sep=&quot;
&quot;)
  )
</pre>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://fronkonstin.com/2017/01/16/the-meeting-point-locator/"> R – Fronkonstin</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/5KdnvNmr-g0"></a></div>