1`plotmap` <-structure(function# easy to use wrapper function 2###note the similarity in name to PBSmapping::plotMap 3###This function is the workhorse of the package RgoogleMaps. It overlays plot on background image of map tile. 4 5( 6 lat, ##<< latitude values to be overlaid OR string to be geocoded OR named vector (lat,lon)! 7 lon, ##<< longitude values to be overlaid 8 map, ##<< optional map object 9 zoom = NULL, ##<< Google maps zoom level 10 API = c("google","OSM","bing", "google2")[1], ##<< choice of map tile API 11 maptype = c("roadmap","mobile","satellite","terrain","hybrid","mapmaker-roadmap","mapmaker-hybrid")[2], ##<< defines the type of map to construct. There are several possible maptype values, including satellite, terrain, hybrid, and mobile. 12 destfile, ##<< File to save the map image to 13 data, ##<< data to look up variables in 14 alpha = 1, ##<< opacity 15 col = 1, ##<< plot color 16 apiKey = NULL, ##<< optional API key (allows for higher rate of downloads for Google); mandatory for Bing maps 17 verbose = 0, ##<< level of verbosity 18 ... ##<< further arguments to be passed to \code{PlotOnStaticMap} 19){ 20 21 # argnames <- names(as.list(match.call(expand.dots = FALSE)[-1])) 22 # arguments <- as.list(match.call()[-1]) 23 # env <- parent.frame() 24 # args <- as.list(match.call(expand.dots = TRUE)[-1]) 25 # argsgiven <- names(args) 26 #browser() 27 # 28 # if ("col" %in% argsgiven) 29 # col <- eval(args$col) 30 31 32 if (!missing(data)) { 33 lon = data[, deparse(substitute(lon))] 34 lat = data[, deparse(substitute(lat))] 35 #for (v in 1:length(data)) assign(names(data)[v], data[[v]]) 36 # browser() 37 # lat=with(incidents,lat) 38 # lon=with(incidents,lon) 39 #col=with(incidents,col) 40 if (is.character(col)) col=data[,col] 41 # 42 #attach(data, -1, warn.conflicts = FALSE) # bad idea! 43 } 44 if (missing(destfile)) destfile = tempfile() 45 46 if (is.character(lat) & missing(map)){ 47 if (API == "google") { 48 map = GetMap(getGeoCode(lat), zoom = zoom,maptype=maptype,destfile=destfile) 49 } else if (API == "OSM") { 50 map = GetOsmMap(getGeoCode(lat), zoom = zoom,maptype=maptype,destfile=destfile) 51 } else if (API == "bing") { 52 map = GetBingMap(getGeoCode(lat), zoom = zoom, maptype=maptype, destfile=destfile,apiKey=apiKey) 53 } 54 #browser() 55 PlotOnStaticMap(MyMap=map) 56 invisible(map) 57 return(map) 58 } 59 if ((is.numeric(lat) | is.list(lat)) & length(lat) == 2 ){ 60 if (all(names(lat) %in% c("lat", "lon"))) { 61 lon = as.numeric(lat["lon"]) 62 lat = as.numeric(lat["lat"]) 63 } else {#assume correct order 64 print("NOTE: Assuming lat/lon order in lat vector!") 65 lon = as.numeric(lat[2]) 66 lat = as.numeric(lat[1]) 67 } 68 } 69 if (is.numeric(lat) & is.numeric(lon) ){ 70 bb=qbbox(lat,lon) 71 if (missing(map)){ 72 if (missing(zoom)){ 73 zoom <- min(MaxZoom(bb$latR, bb$lonR, c(720,720))) 74 if (API == "google") { 75 map = GetMap.bbox(bb$lonR, bb$latR, maptype=maptype,destfile=destfile) 76 } else if (API == "OSM") { 77 #map = GetOsmMap(lonR= bb$lonR, latR =bb$latR, zoom = zoom,maptype=maptype,destfile=destfile) 78 map = GetMapTiles(lonR=bb$lonR, latR=bb$latR,zoom=zoom, verbose=verbose) 79 #browser() 80 } else if (API == "google2") { 81 #map = GetOsmMap(lonR= bb$lonR, latR =bb$latR, zoom = zoom,maptype=maptype,destfile=destfile) 82 map = GetMapTiles(lonR=bb$lonR, latR=bb$latR,zoom=zoom, verbose=verbose, 83 urlBase = "http://mt1.google.com/vt/lyrs=m", 84 tileDir= "~/mapTiles/Google/") 85 #browser() 86 } else if (API == "bing") { 87 bbM=do.call("cbind",bb); 88 ll= bbM[1,] #lower left corner 89 ur= bbM[2,] #upper right corner 90 map = GetBingMap(mapArea=c(ll,ur), zoom = zoom, maptype=maptype, destfile=destfile,apiKey=apiKey) 91 } 92 93 } else { 94 center=c(lat=mean(lat,na.rm=TRUE),lon=mean(lon,na.rm=TRUE)) 95 if (API == "google") { 96 map = GetMap(center=center, zoom = zoom,maptype=maptype,destfile=destfile) 97 } else if (API == "OSM") { 98 #map = GetOsmMap(center=center, zoom = zoom,maptype=maptype,destfile=destfile) 99 map = GetMapTiles(lonR=bb$lonR, latR=bb$latR,zoom=zoom, verbose=verbose) 100 #browser() 101 } else if (API == "google2") { 102 #map = GetOsmMap(lonR= bb$lonR, latR =bb$latR, zoom = zoom,maptype=maptype,destfile=destfile) 103 map = GetMapTiles(lonR=bb$lonR, latR=bb$latR,zoom=zoom, verbose=verbose, 104 urlBase = "http://mt1.google.com/vt/lyrs=m", 105 tileDir= "~/mapTiles/Google/") 106 #browser() 107 } else if (API == "bing") { 108 map = GetBingMap(center=center, zoom = zoom, maptype=maptype, destfile=destfile,apiKey=apiKey) 109 } 110 111 } 112 } 113 if (verbose>1) browser() 114 plotclr = col 115 if (alpha < 1 & !all(is.na(as.numeric(col)))) plotclr = AddAlpha(as.numeric(col), alpha = alpha, verbose = 0) 116 if (class(map) == "mapTiles") { 117 PlotOnMapTiles(map,lat=lat,lon=lon,col=plotclr, ...) 118 } else {#if (class(map) == "staticMap") { 119 PlotOnStaticMap(MyMap=map, lat=lat,lon=lon, col=plotclr, ...) 120 } 121 invisible(map) 122 } 123}, ex = function(){ 124 if (0){ 125 #####################Google maps############################# 126 mapBG1 = plotmap("Brandenburg Gate, Berlin", zoom = 15) 127 128 #####################bing maps############################# 129 130 #for bing maps you will need your own API key, 131 #sign up at https://msdn.microsoft.com/en-us/library/ff428642.aspx 132 apiKey = scan("bingAPIkey.txt",what="") 133 mapBG2 = plotmap("Brandenburg Gate, Berlin", zoom = 15, API = "bing", apiKey=apiKey) 134 135 latlon <- cbind.data.frame(lat = c(38.898648,38.889112, 38.880940), 136 lon = c(-77.037692, -77.050273, -77.03660)); 137 138 139 map3 = plotmap(lat = latlon$lat, lon = latlon$lon, API = "bing", apiKey=apiKey, 140 col = "purple", pch="X",cex=1.5) 141 142 143 #####################OSM maps############################# 144 map4 = plotmap(lat = latlon$lat, lon = latlon$lon, API = "OSM", zoom=15, 145 col = "purple", pch="X",cex=1.5) 146 } 147}) 148