Javascript 如何创建多个存储?
下面我发布了一些从一篇文章中编辑的代码。我只能在地图上显示一个商店的位置。然而,我希望有多个位置,是目前 代码如下:Javascript 如何创建多个存储?,javascript,maps,bing-maps,Javascript,Maps,Bing Maps,下面我发布了一些从一篇文章中编辑的代码。我只能在地图上显示一个商店的位置。然而,我希望有多个位置,是目前 代码如下: <div id="manualEntry"> Your current location <input id="manualAddress" type="text" style="width: 500px" /> <input id="getManualDirections" type="button" value="
<div id="manualEntry">
Your current location
<input id="manualAddress" type="text" style="width: 500px" />
<input id="getManualDirections" type="button" value="Get Directions" />
</div>
<div id="mapContainer" style="height: 500px">
<div style="float: left">
<div id="directionsMap" style="float: none; position: relative; width: 720px; height: 400px">
</div>
</div>
<div id="directionsList" style="float: left; overflow: auto; width: 250px; height: 400px">
</div>
</div>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
$(function () {
var map = null;
var directionsManager = null;
var location = null;
var STORE_LOCATION = "San Jose, CA";
showManualEntry();
$("#askPermission").hide();
loadMap();
// Get the location
var options = {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 2000
};
navigator.geolocation.getCurrentPosition(showPosition, positionError, options);
function loadMap() {
// Initialize the map
if (!map) {
map = new Microsoft.Maps.Map(document.getElementById("directionsMap"),
{ credentials: "YOUR_BING_MAPS_KEY" });
}
}
function showPosition(position) {
map.entities.clear();
if (position) {
location = position.coords;
map.setView({ zoom: 15, center: new Microsoft.Maps.Location(location.lattitude, location.longitude) })
}
if (!directionsManager) {
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', { callback: createDirectionsManager });
}
else {
createDirectionsManager();
}
}
function createDirectionsManager() {
var displayMessage;
if (!directionsManager) {
directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
displayMessage = 'Directions Module loaded\n';
displayMessage += 'Directions Manager loaded';
}
directionsManager.resetDirections();
directionsErrorEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', directionsError);
directionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', directionsUpdated);
createDrivingRoute(location);
}
function directionsUpdated() {
// Show Success message if required
}
function directionsError(args) {
// Show Error message if required
}
function createDrivingRoute(coords) {
if (!directionsManager) { createDirectionsManager(); }
directionsManager.resetDirections();
// Set Route Mode to driving
directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
var fromWayPoint = null;
if (coords != null) {
fromWayPoint = new Microsoft.Maps.Directions.Waypoint(
{
location: new Microsoft.Maps.Location(coords.latitude, coords.longitude)
});
directionsManager.addWaypoint(fromWayPoint);
}
else {
fromWayPoint = new Microsoft.Maps.Directions.Waypoint({ address: $("#manualAddress").val() });
directionsManager.addWaypoint(fromWayPoint);
}
var toWayPoint = new Microsoft.Maps.Directions.Waypoint({ address: STORE_LOCATION });
directionsManager.addWaypoint(toWayPoint);
// Set the element in which the itinerary will be rendered
directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsList') });
directionsManager.calculateDirections();
}
function showManualEntry() {
$("#manualEntry").show();
}
$("#getManualDirections").click(function () {
loadMap();
showPosition(null);
});
function positionError(position) {
switch (position.code) {
case 1:
showManualEntry();
break;
case 2:
showManualEntry();
break;
case 3:
showManualEntry();
break;
default:
break;
}
}
});
</script>
您的当前位置
$(函数(){
var-map=null;
var directionmanager=null;
var位置=空;
var STORE_LOCATION=“加利福尼亚州圣何塞”;
showManualEntry();
$(“#askPermission”).hide();
loadMap();
//获取位置
变量选项={
EnableHighAccurance:正确,
超时:20000,
最高年龄:2000
};
navigator.geolocation.getCurrentPosition(显示位置、位置错误、选项);
函数loadMap(){
//初始化映射
如果(!map){
map=new Microsoft.Maps.map(document.getElementById(“directionsMap”),
{凭证:“你的地图”});
}
}
功能显示位置(位置){
map.entities.clear();
如果(职位){
位置=position.coords;
setView({zoom:15,center:new Microsoft.Maps.Location(Location.latitude,Location.longitude)})
}
如果(!DirectionManager){
Microsoft.Maps.loadModule('Microsoft.Maps.Directions',{callback:createDirectionsManager});
}
否则{
createDirectionsManager();
}
}
函数createDirectionsManager(){
var显示消息;
如果(!DirectionManager){
directionsManager=新的Microsoft.Maps.Directions.directionsManager(地图);
displayMessage='Directions Module loaded\n';
displayMessage+=“已加载方向管理器”;
}
directionsManager.resetDirections();
DirectionErrorEventObj=Microsoft.Maps.Events.addHandler(DirectionManager,'DirectionError',DirectionError);
directionsUpdatedEventObj=Microsoft.Maps.Events.addHandler(directionsManager,'directionsUpdated',directionsUpdated);
创建驾驶路线(位置);
}
函数方向更新(){
//如果需要,显示成功消息
}
函数方向错误(args){
//如果需要,显示错误消息
}
函数createDrivingRoute(coords){
如果(!DirectionManager){CreateDirectionManager();}
directionsManager.resetDirections();
//将路线模式设置为驾驶模式
directionsManager.setRequestOptions({routeMode:Microsoft.Maps.Directions.routeMode.driving});
var fromWayPoint=null;
if(coords!=null){
fromWayPoint=新建Microsoft.Maps.Directions.Waypoint(
{
位置:新的Microsoft.Maps.location(坐标.纬度,坐标.经度)
});
directionsManager.addWaypoint(从航路点);
}
否则{
fromWayPoint=新的Microsoft.Maps.Directions.Waypoint({address:$(“#manualAddress”).val()});
directionsManager.addWaypoint(从航路点);
}
var toWayPoint=new Microsoft.Maps.Directions.Waypoint({address:STORE_LOCATION});
directionsManager.addWaypoint(toWayPoint);
//设置将在其中呈现行程的元素
directionsManager.setRenderOptions({InventureContainer:document.getElementById('directionsList'))});
DirectionManager.calculateDirections();
}
函数showManualEntry(){
$(“#手动输入”).show();
}
$(“#getManualDirections”)。单击(函数(){
loadMap();
显示位置(空);
});
功能位置错误(位置){
开关(位置代码){
案例1:
showManualEntry();
打破
案例2:
showManualEntry();
打破
案例3:
showManualEntry();
打破
违约:
打破
}
}
});
以下是文章:
正如尼古拉斯指出的那样,您拥有的代码只会在地图上显示一个位置。您首先需要的是存储的数据集。许多Bing地图客户使用Bing空间数据服务存储他们的存储数据,并将其作为空间REST服务公开。一旦您拥有与所需架构匹配的数据源文件,您就可以使用Bing地图门户将其上载到您的帐户,如下所述: 一旦有了数据源,您就可以轻松地查询数据源,并让它显示您附近的门店位置。以下是一些例子: 如果数据库中有数据,您可能更愿意使用自定义web服务公开这些数据。如果是这样的话,那么看看这些博客帖子:
我刚刚检查了你的代码和文章,我想这里只有一个店址。如果位置服务检测到,则另一个位置是用户的位置。这样你永远不会得到多个位置。