欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

百度地图实时检测位置,数据库获取经纬度显示在地图上

程序员文章站 2022-03-16 18:25:17
...
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=**"></script>
	<title>浏览器定位</title>
	<link rel="shortcut icon" href="${pageContext.request.contextPath}/assets/img/favicon.ico" type="image/x-icon" />
	<link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/shouye.css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/index.css" />
	<link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/base.css" />
</head>
<body>
<jsp:include page="./common/userheader.jsp"></jsp:include>
<div class="headerbg">
  <div class="AllWrap headerInfo">
    <div class="logo">
    <img alt="" src="${pageContext.request.contextPath}/assets/img/img.jpg"> &nbsp;	&nbsp;	&nbsp;	
    <img alt="" src="${pageContext.request.contextPath}/assets/img/logo.jpg"></div>
    <div id="box" style="display:block;margin-left:500px">
      <form action="">
        <input type="text" id="search1" name="search" placeholder="请输入关键字">
        <input type="submit" id="search" value="搜索">
      </form>
    </div>
  </div>
</div>
<jsp:include page="./common/partment.jsp"></jsp:include>
<div style="width:730px;margin:auto;margin-top:10px">   
        要查询的地址:<input id="text_" type="text"  style="margin-right:100px;"/>
        查询结果(经纬度):<input id="result_" type="text" />
        <input type="button" value="查询周围土地" onclick="searchByStationName();"/>
        <div id="container"
            style="position: absolute;
                margin-top:30px; 
                width: 730px; 
                height: 590px; 
                top: 50; 
                border: 1px solid gray;
                overflow:hidden;">
        </div>
    </div>
<footer class="py-4 bg-foot footer-text" style="margin-top: 700px">
  <center>
    <span class="text-"> Copyright &copy; 2019 南京龙渊公司 All Rights Reserved.</span>
  </center>
</footer>
</body>
<script src="${pageContext.request.contextPath}/assets/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    var map = new BMap.Map("container");
    var point = new BMap.Point(116.331398,39.897445);
    map.centerAndZoom(point, 14);//设置开始的时候所在的位置
    map.enableScrollWheelZoom();    //启用滚轮放大缩小,默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽,默认禁用

    map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
    map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
    map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT }));   //右下角,打开

    var localSearch = new BMap.LocalSearch(map);

    localSearch.enableAutoViewport(); //允许自动调节窗体大小

    var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r){
		if(this.getStatus() == BMAP_STATUS_SUCCESS){
			var mk = new BMap.Marker(r.point);
			map.addOverlay(mk);
			map.panTo(r.point);
            //用户登录获取当前的位置  
            var label = new BMap.Label("您的当前位置",{offset:new BMap.Size(20,-10)});
            	mk.setLabel(label);
    		    var gc = new BMap.Geocoder();
	            var pointAdd = new BMap.Point(r.point.lng, r.point.lat);
	            gc.getLocation(pointAdd, function(rs){
	                // 百度地图解析城市名
	                //或者其他信息
	                console.log(rs);
			})

		}
		else {
			alert('failed'+this.getStatus());
		}        
	},{enableHighAccuracy: true})
		
	$.get("http://127.0.0.1:8080/happy-farm/map/landlist",function(data){
		console.log(data)

	
		for(var item in data ){
        //新加入每一个点,可以算作土地的标签
        	console.log(data[item].latitude)
        	var new_point = new BMap.Point(data[item].longitude,data[item].latitude);
        
			var marker = new BMap.Marker(new_point);  // 创建标注
	        var label = new BMap.Label("土地位置",{offset:new BMap.Size(25,-10)});
	        marker.setLabel(label);
			map.addOverlay(marker);    
		}// 将标注添加到地图中
		
		
	})
	

	
	function searchByStationName() {
		   map.clearOverlays();//清空原来的标注
		    var keyword = document.getElementById("text_").value;
		    localSearch.setSearchCompleteCallback(function (searchResult) {
		        var poi = searchResult.getPoi(0);
		        document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
		        map.centerAndZoom(poi.point, 13);
		        var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注,为要查询的地方对应的经纬度
		      
		        map.addOverlay(marker);
		        var content = document.getElementById("text_").value + "<br/><br/>经度:" + poi.point.lng + "<br/>纬度:" + poi.point.lat;
		        var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>");
		        marker.addEventListener("click", function () { this.openInfoWindow(infoWindow); });
		         marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
		           var label = new BMap.Label("您所选的位置:"+keyword,{offset:new BMap.Size(25,-10)});
		           marker.setLabel(label);
		         
		         
		     	$.get("http://127.0.0.1:8080/happy-farm/map/landlist",function(data){
		    		console.log(data)
		    		for(var item in data ){
		            //新加入每一个点,可以算作土地的标签
		            	console.log(data[item].latitude)
		            	var new_point = new BMap.Point(data[item].longitude,data[item].latitude);
		            
		    			var marker = new BMap.Marker(new_point);  // 创建标注
		    			
		    			  
		    			 marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
				           map.addOverlay(marker);  
		    		}// 将标注添加到地图中
		    		
		    		
		    	})
		         
		    });
		    localSearch.search(keyword);
		} 
	



</script>
</html>


百度地图实时检测位置,数据库获取经纬度显示在地图上

相关标签: 百度地图