forked from badbye/baidumap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
616 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf8" /> | ||
<meta name="keywords" content="百度地图,百度地图API,百度地图自定义工具,百度地图所见即所得工具" /> | ||
<meta name="description" content="百度地图API自定义地图,帮助用户在可视化操作下生成百度地图" /> | ||
<title>百度地图API自定义地图</title> | ||
<!--引用百度地图API--> | ||
<style type="text/css"> | ||
html,body{margin:0;padding:0;} | ||
.iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hidden;padding-right:13px;white-space:nowrap} | ||
.iw_poi_content {font:12px arial,sans-serif;overflow:visible;padding-top:4px;white-space:-moz-pre-wrap;word-wrap:break-word} | ||
</style> | ||
<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script> | ||
</head> | ||
|
||
<body> | ||
<!--百度地图容器--> | ||
<div style="width:697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div> | ||
</body> | ||
<script type="text/javascript"> | ||
//创建和初始化地图函数: | ||
function initMap(){ | ||
createMap();//创建地图 | ||
setMapEvent();//设置地图事件 | ||
addMapControl();//向地图添加控件 | ||
addMarker();//向地图中添加marker | ||
} | ||
|
||
//创建地图函数: | ||
function createMap(){ | ||
var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图 | ||
var point = new BMap.Point(116.395645,39.929986);//定义一个中心点坐标 | ||
map.centerAndZoom(point,12);//设定地图的中心点和坐标并将地图显示在地图容器中 | ||
window.map = map;//将map变量存储在全局 | ||
} | ||
|
||
//地图事件设置函数: | ||
function setMapEvent(){ | ||
map.enableDragging();//启用地图拖拽事件,默认启用(可不写) | ||
map.enableScrollWheelZoom();//启用地图滚轮放大缩小 | ||
map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写) | ||
map.enableKeyboard();//启用键盘上下左右键移动地图 | ||
} | ||
|
||
//地图控件添加函数: | ||
function addMapControl(){ | ||
//向地图中添加缩放控件 | ||
var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE}); | ||
map.addControl(ctrl_nav); | ||
//向地图中添加缩略图控件 | ||
var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_TOP_RIGHT,isOpen:1}); | ||
map.addControl(ctrl_ove); | ||
//向地图中添加比例尺控件 | ||
var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT}); | ||
map.addControl(ctrl_sca); | ||
} | ||
|
||
//标注点数组 | ||
var markerArr = [{title:"北京动物园",content:"来看狮子老虎大象猴子- -",point:"116.34304|39.949459",isOpen:0,icon:{w:21,h:21,l:0,t:0,x:6,lb:5}} | ||
,{title:"工人体育场",content:"oh,工体",point:"116.455724|39.937953",isOpen:0,icon:{w:21,h:21,l:0,t:0,x:6,lb:5}} | ||
]; | ||
//创建marker | ||
function addMarker(){ | ||
for(var i=0;i<markerArr.length;i++){ | ||
var json = markerArr[i]; | ||
var p0 = json.point.split("|")[0]; | ||
var p1 = json.point.split("|")[1]; | ||
var point = new BMap.Point(p0,p1); | ||
var iconImg = createIcon(json.icon); | ||
var marker = new BMap.Marker(point,{icon:iconImg}); | ||
var iw = createInfoWindow(i); | ||
var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)}); | ||
marker.setLabel(label); | ||
map.addOverlay(marker); | ||
label.setStyle({ | ||
borderColor:"#808080", | ||
color:"#333", | ||
cursor:"pointer" | ||
}); | ||
|
||
(function(){ | ||
var index = i; | ||
var _iw = createInfoWindow(i); | ||
var _marker = marker; | ||
_marker.addEventListener("click",function(){ | ||
this.openInfoWindow(_iw); | ||
}); | ||
_iw.addEventListener("open",function(){ | ||
_marker.getLabel().hide(); | ||
}) | ||
_iw.addEventListener("close",function(){ | ||
_marker.getLabel().show(); | ||
}) | ||
label.addEventListener("click",function(){ | ||
_marker.openInfoWindow(_iw); | ||
}) | ||
if(!!json.isOpen){ | ||
label.hide(); | ||
_marker.openInfoWindow(_iw); | ||
} | ||
})() | ||
} | ||
} | ||
//创建InfoWindow | ||
function createInfoWindow(i){ | ||
var json = markerArr[i]; | ||
var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>"); | ||
return iw; | ||
} | ||
//创建一个Icon | ||
function createIcon(json){ | ||
var icon = new BMap.Icon("http://app.baidu.com/map/images/us_mk_icon.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)}) | ||
return icon; | ||
} | ||
|
||
initMap();//创建和初始化地图 | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf8" /> | ||
<meta name="keywords" content="百度地图,百度地图API,百度地图自定义工具,百度地图所见即所得工具" /> | ||
<meta name="description" content="百度地图API自定义地图,帮助用户在可视化操作下生成百度地图" /> | ||
<title>百度地图API自定义地图</title> | ||
<!--引用百度地图API--> | ||
<style type="text/css"> | ||
html,body{margin:0;padding:0;} | ||
.iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hidden;padding-right:13px;white-space:nowrap} | ||
.iw_poi_content {font:12px arial,sans-serif;overflow:visible;padding-top:4px;white-space:-moz-pre-wrap;word-wrap:break-word} | ||
</style> | ||
<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script> | ||
</head> | ||
|
||
<body> | ||
<!--百度地图容器--> | ||
<div style="width:697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div> | ||
</body> | ||
<script type="text/javascript"> | ||
//创建和初始化地图函数: | ||
function initMap(){ | ||
createMap();//创建地图 | ||
setMapEvent();//设置地图事件 | ||
addMapControl();//向地图添加控件 | ||
addMarker();//向地图中添加marker | ||
} | ||
|
||
//创建地图函数: | ||
function createMap(){ | ||
var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图 | ||
var point = new BMap.Point(116.395645,39.929986);//定义一个中心点坐标 | ||
map.centerAndZoom(point,12);//设定地图的中心点和坐标并将地图显示在地图容器中 | ||
window.map = map;//将map变量存储在全局 | ||
} | ||
|
||
//地图事件设置函数: | ||
function setMapEvent(){ | ||
map.enableDragging();//启用地图拖拽事件,默认启用(可不写) | ||
map.enableScrollWheelZoom();//启用地图滚轮放大缩小 | ||
map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写) | ||
map.enableKeyboard();//启用键盘上下左右键移动地图 | ||
} | ||
|
||
//地图控件添加函数: | ||
function addMapControl(){ | ||
//向地图中添加缩放控件 | ||
var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE}); | ||
map.addControl(ctrl_nav); | ||
//向地图中添加缩略图控件 | ||
var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_TOP_RIGHT,isOpen:1}); | ||
map.addControl(ctrl_ove); | ||
//向地图中添加比例尺控件 | ||
var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT}); | ||
map.addControl(ctrl_sca); | ||
} | ||
|
||
//标注点数组 | ||
var markerArr = [{title:"我的标记",content:"我的备注",point:"116.34304|39.949459",isOpen:0,icon:{w:21,h:21,l:0,t:0,x:6,lb:5}} | ||
,{title:"工人体育场",content:"我的备注",point:"116.455724|39.937953",isOpen:0,icon:{w:21,h:21,l:0,t:0,x:6,lb:5}} | ||
]; | ||
//创建marker | ||
function addMarker(){ | ||
for(var i=0;i<markerArr.length;i++){ | ||
var json = markerArr[i]; | ||
var p0 = json.point.split("|")[0]; | ||
var p1 = json.point.split("|")[1]; | ||
var point = new BMap.Point(p0,p1); | ||
var iconImg = createIcon(json.icon); | ||
var marker = new BMap.Marker(point,{icon:iconImg}); | ||
var iw = createInfoWindow(i); | ||
var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)}); | ||
marker.setLabel(label); | ||
map.addOverlay(marker); | ||
label.setStyle({ | ||
borderColor:"#808080", | ||
color:"#333", | ||
cursor:"pointer" | ||
}); | ||
|
||
(function(){ | ||
var index = i; | ||
var _iw = createInfoWindow(i); | ||
var _marker = marker; | ||
_marker.addEventListener("click",function(){ | ||
this.openInfoWindow(_iw); | ||
}); | ||
_iw.addEventListener("open",function(){ | ||
_marker.getLabel().hide(); | ||
}) | ||
_iw.addEventListener("close",function(){ | ||
_marker.getLabel().show(); | ||
}) | ||
label.addEventListener("click",function(){ | ||
_marker.openInfoWindow(_iw); | ||
}) | ||
if(!!json.isOpen){ | ||
label.hide(); | ||
_marker.openInfoWindow(_iw); | ||
} | ||
})() | ||
} | ||
} | ||
//创建InfoWindow | ||
function createInfoWindow(i){ | ||
var json = markerArr[i]; | ||
var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>"); | ||
return iw; | ||
} | ||
//创建一个Icon | ||
function createIcon(json){ | ||
var icon = new BMap.Icon("http://app.baidu.com/map/images/us_mk_icon.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)}) | ||
return icon; | ||
} | ||
|
||
initMap();//创建和初始化地图 | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Package: baidumap | ||
Title: A package for spatial visualization with Baidu | ||
Version: 0.1 | ||
Authors: yalei Du <[email protected]> | ||
Description: Just like ggmap, but get map from baidu instead of google or openstreet | ||
Depends: | ||
R (>= 3.1.1) | ||
Imports: | ||
ggmap, | ||
RgoogleMaps, | ||
png, | ||
RCurl | ||
License: GPL-2 | ||
LazyData: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Package: baidumap | ||
Title: A package for spatial visualization with Baidu | ||
Version: 0.1 | ||
Authors@R: "yalei Du<[email protected]>" | ||
Description: Just like ggmap, but get map from baidu instead of google or openstreet | ||
Depends: R (>= 3.1.1) | ||
Imports: ggmap, RgoogleMaps, png, RCurl, rjson, XML | ||
License: GPL-2 | ||
LazyData: true | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Generated by roxygen2 (4.0.2): do not edit by hand | ||
|
||
export(getBaiduMap) | ||
export(getCoordinate) | ||
export(getLocation) | ||
importFrom(RCurl,getURL) | ||
importFrom(RgoogleMaps,XY2LatLon) | ||
importFrom(ggmap,ggmap) | ||
importFrom(png,readPNG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#' Get map from Baidu API | ||
#' | ||
#' Take in coordiantes and return the location | ||
#' | ||
#' @param lon longtitude of the center of the map | ||
#' @param lat latitude of the center of the map | ||
#' @param width width of the map | ||
#' @param height height of the map | ||
#' @param zoom map zoom, an integer from 3 (continent) to 21 (building), default value 10 (city) | ||
#' @param scale multiplicative factor for the number of pixels returned. possible values are 2 or anything else. | ||
#' @param color "color" or "bw", color or black-and-white | ||
#' @param messaging logical. whether to print the download messages. | ||
#' @return A ggmap object. a map image as a 2d-array of colors as hexadecimal strings representing pixel fill values. | ||
#' @export | ||
#' @importFrom png readPNG | ||
#' @importFrom RgoogleMaps XY2LatLon | ||
#' @importFrom ggmap ggmap | ||
#' @examples | ||
#' | ||
#' \dontrun{ | ||
#' library(ggmap) | ||
#' ## default map: Beijing | ||
#' p <- getBaiduMap() | ||
#' ggmap(p) | ||
#' | ||
#' ## black-and-white | ||
#' p <- getBaiduMap(color='bw') | ||
#' ggmap(p) | ||
#' | ||
#' ## do not print messages | ||
#' p <- getBaiduMap(messaging = F) | ||
#' } | ||
getBaiduMap = function(lon=116.354431, lat=39.942333, width=400, height = 400, zoom=10, scale=2, color = "color", messaging = TRUE){ | ||
## set url | ||
url_head = "http://api.map.baidu.com/staticimage?" | ||
url = paste0(url_head, "width=", width, "&height=", height, "¢er=", | ||
lon, ",", lat, "&zoom=", zoom) | ||
if (scale == 2) url = paste0(url, "&scale=2") | ||
|
||
## download image | ||
if (!'baiduMapFileDrawer' %in% list.dirs(full.names= F, recursive=F)) { | ||
dir.create('baiduMapFileDrawer') | ||
} | ||
destfile = paste0(lon, ";", lat, ".png") | ||
download.file(url, destfile = paste0("baiduMapFileDrawer/", destfile), | ||
quiet = !messaging, mode = "wb") | ||
if (messaging) message(paste0("Map from URL : ", url)) | ||
|
||
## read image and transform to ggmap obejct | ||
map = readPNG(paste0("baiduMapFileDrawer/", destfile)) | ||
# format file | ||
if(color == "color"){ | ||
map <- apply(map, 2, rgb) | ||
} else if(color == "bw"){ | ||
mapd <- dim(map) | ||
map <- gray(.30 * map[,,1] + .59 * map[,,2] + .11 * map[,,3]) | ||
dim(map) <- mapd[1:2] | ||
} | ||
class(map) <- c("ggmap","raster") | ||
|
||
# map spatial info | ||
ll <- XY2LatLon( | ||
list(lat = lat, lon = lon, zoom = zoom), | ||
-width/2 + 0.5, | ||
-height/2 - 0.5 | ||
) | ||
ur <- XY2LatLon( | ||
list(lat = lat, lon = lon, zoom = zoom), | ||
width/2 + 0.5, | ||
height/2 - 0.5 | ||
) | ||
attr(map, "bb") <- data.frame( | ||
ll.lat = ll[1], ll.lon = ll[2], | ||
ur.lat = ur[1], ur.lon = ur[2] | ||
) | ||
|
||
# transpose | ||
out <- t(map) | ||
out | ||
} |
Oops, something went wrong.