利用Leaflet.js和Node.js开发地图应用指南

需积分: 10 0 下载量 33 浏览量 更新于2024-11-20 收藏 841KB ZIP 举报
该项目包含三个主要的JavaScript文件:index.js、map.js和db.js。其中,index.js是主文件,负责启动服务并监听本地主机的8080端口;map.js文件负责地图逻辑的实现;db.js文件负责数据库连接。 Leaflet.js是一个开源的JavaScript库,专门用于创建交互式地图,它可以与HTML和CSS配合使用,将地图元素轻松集成到网站上。在本项目中,Leaflet.js被用来绘制和管理地图,包括标记点、绘制路线等。 Node.js是一个基于Chrome V8引擎的JavaScript运行环境,它使用事件驱动、非阻塞I/O模型,可以构建快速、可扩展的网络应用程序。在本项目中,Node.js用于搭建服务器环境,监听本地主机的8080端口,并响应客户端请求。 在项目中,db.js文件用于建立与数据库的连接。它包含一个SQL查询语句,用于从数据库中检索数据。SQL查询语句是:\"select id,(select concat_ws('/',lat,lon) from nodes_test where id=srcid) as latlon1,(select concat_ws('/',lat,lon) from nodes_test where id=trgid) as latlon2,来自edges_test的速度\"。这个查询语句用于获取地图上的节点和边的相关信息,以及边的速度信息。 通过以上四个主要的文件,我们可以实现一个基本的地图图功能,包括地图的显示、节点和边的绘制,以及速度信息的展示。这是一个典型的前端和后端分离的Web应用程序,前端使用Leaflet.js展示地图,后端使用Node.js提供数据接口。" 知识点: 1. Leaflet.js: 一个开源的JavaScript库,用于创建交互式地图,支持多种地图服务提供商,具有丰富的插件和API,可以实现地图的定制化开发。 2. node.js: 一个基于Chrome V8引擎的JavaScript运行环境,用于构建可扩展的网络应用,它支持异步编程,使用事件驱动、非阻塞I/O模型来处理网络请求。 3. 地图逻辑实现: 通过JavaScript编写地图逻辑,包括地图的加载、缩放、平移等交互功能,以及在地图上添加标记点、绘制路径线、展示速度等信息。 4. 数据库连接与数据查询: 通过编写SQL查询语句与数据库进行连接和数据交互,获取地图上节点和边的相关信息,支持地图的动态展示和数据更新。 5. 网络编程: 在node.js中处理HTTP请求和响应,为前端提供RESTful接口,实现前后端分离的开发模式。 6. 本地主机端口监听: 在node.js服务中设置监听本地主机指定端口(如8080端口),使本地开发的Web应用可以通过浏览器进行访问。 7. 异步编程: 在node.js中通过回调函数、Promise、async/await等方式处理异步操作,提高程序执行效率和用户体验。 8. 数据库操作: 在db.js文件中通过Node.js访问数据库,执行SQL语句,进行数据的查询、更新、插入和删除操作。 9. 地图功能定制: 根据项目需求定制地图功能,例如展示不同类型的标记点、绘制多条路径线、展示不同速度级别的信息等。 10. 文件模块化开发: 将项目功能拆分成不同的JavaScript模块(如index.js、map.js和db.js),各自处理特定的业务逻辑,提高代码的可维护性和复用性。

class Path(object): def __init__(self,path,distancecost,timecost): self.__path = path self.__distancecost = distancecost self.__timecost = timecost #路径上最后一个节点 def getLastNode(self): return self.__path[-1] #获取路径路径 @property def path(self): return self.__path #判断node是否为路径上最后一个节点 def isLastNode(self, node): return node == self.getLastNode() #增加加点和成本产生一个新的path对象 def addNode(self, node, dprice, tprice): return Path(self.__path+[node],self.__distancecost + dprice,self.__timecost + tprice) #输出当前路径 def printPath(self): for n in self.__path: if self.isLastNode(node=n): print(n) else: print(n, end="->") print(f"最短路径距离(self.__distancecost:.0f)m") print(f"红绿路灯个数(self.__timecost:.0f)个") #获取路径总成本的只读属性 @property def dCost(self): return self.__distancecost @property def tCost(self): return self.__timecost class DirectedGraph(object): def __init__(self, d): if isinstance(d, dict): self.__graph = d else: self.__graph = dict() print('Sth error') #通过递归生成所有可能的路径 def __generatePath(self, graph, path, end, results, distancecostIndex, timecostIndex): current = path.getLastNode() if current == end: results.append(path) else: for n in graph[current]: if n not in path.path: self.__generatePath(graph, path.addNode(n,self.__graph[path.getLastNode()][n][distancecostIndex][timecostIndex]), end, results, distancecostIndex, timecostIndex) #搜索start到end之间时间或空间最短的路径,并输出 def __searchPath(self, start, end, distancecostIndex, timecostIndex): results = [] self.__generatePath(self.__graph, Path([start],0,0), end, results,distancecostIndex,timecostIndex) results.sort(key=lambda p: p.distanceCost) results.sort(key=lambda p: p.timeCost) print('The {} shortest path from '.format("spatially" if distancecostIndex==0 else "temporally"), start, ' to ', end, ' is:', end="") print('The {} shortest path from '.format("spatially" if timecostIndex==0 else "temporally"), start, ' to ', end, ' is:', end="") results[0].printPath() #调用__searchPath搜索start到end之间的空间最短的路径,并输出 def searchSpatialMinPath(self,start, end): self.__searchPath(start,end,0,0) #调用__searc 优化这个代码

154 浏览量

import random class Path(): def __init__(self, path, cost): self.__path = path self.__cost = cost # 获取路径上最后一个节点(T) def getLastNode(self): return self.__path[-1] # 获取路径的只读属性 @property def path(self): #这个函数有什么用 return self.__path # 判断node是否为最后一个节点 def isLastNode(self, node): return node == self.getLastNode() # 增加节点和成本 def addNode(self, node, price): return Path(self.__path+[node], self.__cost+price) # 输出当前路径 def printPath(self): for n in self.__path: if self.isLastNode(n): print(n) else: print(n, end="-->") print("Cost is: "+str(self.__cost)) # 获取成本的只读属性 @property def travelCost(self): return self.__cost class DirectedGraph(): def __init__(self,d): self.__graph = d def __generatePath(self, graph, path, end, results, costIndex): current = path.getLastNode() if current == end: results.append(path) else: for n in graph[current]: if n not in path.path: self.__generatePath(graph, path.addNode(n, self.__graph[path.getLastNode()][n][costIndex]), end , results, costIndex) # 搜索start到end之间时间或空间的最短路径,并输出 def searchPath(self, start, end): self._results = [] self.__generatePath(self.__graph, Path([start], 0), end,self._results) self._results.sort(key=lambda x: len(x)) # 调用searchPath搜索start到end之间空间最短的路径,并输出 def searchMinPath(self, start, end): self.searchPath(start, end) print("共有{}条路径".format(len(self._results))) # 计算路径数 for path in self._results: #print(path) distance = 0 light_points=0 time = 0 for i in range(len(path) - 1): point1 = self.__graph[path[i]] point2 = point1[path[i + 1]] distance += point2[0] time += (point2[0]/point2[1]) light_points+=point2[2] path.append(distance) path.append(light_points)

2023-05-29 上传

优化该代码class Path(object): def __init__(self,path,cost1,cost2): self.__path = path self.__cost1 = cost1 self.__cost2 = cost2 #路径上最后一个节点 def getLastNode(self): return self.__path[-1] #获取路径路径 @property def path(self): return self.__path #判断node是否为路径上最后一个节点 def isLastNode(self, node): return node == self.getLastNode() #增加加点和成本产生一个新的path对象 def addNode(self, node, price1,price2): return Path(self.__path+[node],self.__cost1+ price1,self.__cost2+ price2) #输出当前路径 def printPath(self): global num #将num作为循环次数,即红绿灯数量 global distance num = 0 for n in self.__path: if self.isLastNode(node=n): print(n) else: print(n, end="->") num += 1 print("全程约为 {:.4}公里".format(str(self.__cost1))) print("时间大约为 {}分钟".format(str(self.__cost2))) print("需要经过{}个红绿灯".format(num)) distance = self.__cost1 #获取路径总成本的只读属性 @property def travelCost1(self): return self.__cost1 @property def travelCost2(self): return self.__cost2 class DirectedGraph(object): def __init__(self, d): if isinstance(d, dict): self.__graph = d else: self.__graph = dict() print('Sth error') def __generatePath(self, graph, path, end, results): #current = path[-1] current = path.getLastNode() if current == end: results.append(path) else: for n in graph[current]: #if n not in path: if n not in path.path: #self.__generatePath(graph, path + [n], end, results) self.__generatePath(graph, path.addNode(n,self.__graph[path.getLastNode()][n][0],self.__graph[path.getLastNode()][n][1]),end, results) #self.__generatePath(graph,使其能够保存输入记录并且能够查询和显示

130 浏览量

class Path(object): def __init__(self,path,cost1,cost2): self.__path = path self.__cost1 = cost1 self.__cost2 = cost2 #路径上最后一个节点 def getLastNode(self): return self.__path[-1] #获取路径路径 @property def path(self): return self.__path #判断node是否为路径上最后一个节点 def isLastNode(self, node): return node == self.getLastNode() #增加加点和成本产生一个新的path对象 def addNode(self, node, price1,price2): return Path(self.__path+[node],self.__cost1+ price1,self.__cost2+ price2) #输出当前路径 def printPath(self): global num #将num作为循环次数,即红绿灯数量 global distance num = 0 for n in self.__path: if self.isLastNode(node=n): print(n) else: print(n, end="->") num += 1 print("全程约为 {:.4}公里".format(str(self.__cost1))) print("时间大约为 {}分钟".format(str(self.__cost2))) print("需要经过{}个红绿灯".format(num)) distance = self.__cost1 #获取路径总成本的只读属性 @property def travelCost1(self): return self.__cost1 @property def travelCost2(self): return self.__cost2 class DirectedGraph(object): def __init__(self, d): if isinstance(d, dict): self.__graph = d else: self.__graph = dict() print('Sth error') def __generatePath(self, graph, path, end, results): #current = path[-1] current = path.getLastNode() if current == end: results.append(path) else: for n in graph[current]: #if n not in path: if n not in path.path: #self.__generatePath(graph, path + [n], end, results) self.__generatePath(graph, path.addNode(n,self.__graph[path.getLastNode()][n][0],self.__graph[path.getLastNode()][n][1]),end, results) #self.__gener给该代码加一个可以保存输入记录并且能够查询显示的功能

2023-06-13 上传