腾讯地图API使用

工作需求:

由于同一栋大楼有多个机构,所以在地图上,这些机构的点都重叠在一起,需要添加一个地图功能,使得移动一点,改变机构经纬度,来让他们的lat lng不同

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>设置marker属性</title>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }
        body,
        button,
        input,
        select,
        textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        p {
            width: 603px;
            padding-top: 3px;
            margin-top: 10px;
            overflow: hidden;
        }
        .btn {
            width: 112px;
        }
    </style>
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=M7VBZ-476RJ-TSIFB-FMZIF-YG4OT-RFFNG"></script>
   
 
</head>
 
<body onLoad="init()">
    <script>
        function init() {
        	let lat = getQueryString("lat");
        	let lng = getQueryString("lng");
        	let id = getQueryString("id");
        	/* let address = decodeURI(getQueryString("address"),"utf-8"); */
        	
            var center = new qq.maps.LatLng(lat, lng);
            var map = new qq.maps.Map(document.getElementById("container"), {
                center: center,
                zoom: 30
            });
            //创建一个Marker
            var marker = new qq.maps.Marker({
                //设置Marker的位置坐标
                position: center,
                //设置显示Marker的地图
                map: map
            });
 
            //设置Marker的可见性,为true时可见,false时不可见,默认属性为true
            marker.setVisible(true);
            //设置Marker的动画属性为从落下
            marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
            //设置Marker是否可以被拖拽,为true时可拖拽,false时不可拖拽,默认属性为false
            marker.setDraggable(true);
            ////设置Marker自定义图标的属性,size是图标尺寸,该尺寸为显示图标的实际尺寸,origin是切图坐标,该坐标是相对于图片左上角默认为(0,0)的相对像素坐标,anchor是锚点坐标,描述经纬度点对应图标中的位置
            var anchor = new qq.maps.Point(0, 39),
                size = new qq.maps.Size(42, 68),
                origin = new qq.maps.Point(0, 0),
                icon = new qq.maps.MarkerImage(
                    "https://open.map.qq.com/doc/img/nilt.png",
                    size,
                    origin,
                    anchor
                );
            marker.setIcon(icon);
            //设置Marker阴影图片属性,size是图标尺寸,该尺寸为显示图标的实际尺寸,origin是切图坐标,该坐标是相对于图片左上角默认为(0,0)的相对像素坐标,anchor是锚点坐标,描述经纬度点对应图标中的位置
            var anchorb = new qq.maps.Point(3, -30),
                sizeb = new qq.maps.Size(42, 11),
                origin = new qq.maps.Point(0, 0),
                iconb = new qq.maps.MarkerImage(
                    "https://open.map.qq.com/doc/img/nilb.png",
                    sizeb,
                    origin,
                    anchorb
                );
            marker.setShadow(iconb);
            //设置标注的名称,当鼠标划过Marker时显示
            marker.setTitle("测试");
 
            //添加信息窗口
            var info = new qq.maps.InfoWindow({
                map: map
            });
            //获取标记的可拖动属性
            info.open();
            info.setContent("经度:"+lng+"——纬度:"+lat);
            info.setPosition(marker.getPosition());
            //标记Marker点击事件
            qq.maps.event.addListener(marker, 'click', function() {
                info.open();
                info.setContent('<div style="text-align:center;white-space:nowrap;' +
                    'margin:10px;">单击标记</div>');
                info.setPosition(marker.getPosition());
            });
            //设置Marker停止拖动事件
            qq.maps.event.addListener(marker, 'dragend', function() {
                info.open();
                info.setPosition(marker.getPosition());
                info.setContent('<div style="text-align:center;white-space:nowrap;' +
                    'margin:10px;">改变成功!当前的经度为:'+marker.getPosition().lng+'——纬度为:'+marker.getPosition().lat+'</div>');
                //getPosition()  返回Marker的位置
                
                //把修改后的经纬度保存回数据库
                let forumAgency = new Object();
                forumAgency.agencyLng=marker.getPosition().lng;
                forumAgency.agencyLat=marker.getPosition().lat;
                forumAgency.id=id;
                Ajax.request({
            		url: "../forumagency/updatelatlng",
            		params: JSON.stringify(forumAgency),
 				    contentType: "application/json",
            		type: "POST",
            		contentType: "application/json",
            		successCallback: function (r) {
            			console.log(r)
            		}
            	}); 
            });
 
        }
    </script>
    <div style="width:100%;height:700px" id="container"></div>

</body>
 
</html>


还有其他几个能用的不同功能的model

可拖动标签地图

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>设置marker属性</title>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }
        body,
        button,
        input,
        select,
        textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        p {
            width: 603px;
            padding-top: 3px;
            margin-top: 10px;
            overflow: hidden;
        }
        .btn {
            width: 112px;
        }
    </style>
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=M7VBZ-476RJ-TSIFB-FMZIF-YG4OT-RFFNG"></script>
 
</head>
 
<body onLoad="init()">
    <script>
        function init() {
            var center = new qq.maps.LatLng(39.982163, 116.306070);
            var map = new qq.maps.Map(document.getElementById("container"), {
                center: center,
                zoom: 16
            });
            //创建一个Marker
            var marker = new qq.maps.Marker({
                //设置Marker的位置坐标
                position: center,
                //设置显示Marker的地图
                map: map
            });
 
            //设置Marker的可见性,为true时可见,false时不可见,默认属性为true
            marker.setVisible(true);
            //设置Marker的动画属性为从落下
            marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
            //设置Marker是否可以被拖拽,为true时可拖拽,false时不可拖拽,默认属性为false
            marker.setDraggable(true);
            ////设置Marker自定义图标的属性,size是图标尺寸,该尺寸为显示图标的实际尺寸,origin是切图坐标,该坐标是相对于图片左上角默认为(0,0)的相对像素坐标,anchor是锚点坐标,描述经纬度点对应图标中的位置
            var anchor = new qq.maps.Point(0, 39),
                size = new qq.maps.Size(42, 68),
                origin = new qq.maps.Point(0, 0),
                icon = new qq.maps.MarkerImage(
                    "https://open.map.qq.com/doc/img/nilt.png",
                    size,
                    origin,
                    anchor
                );
            marker.setIcon(icon);
            //设置Marker阴影图片属性,size是图标尺寸,该尺寸为显示图标的实际尺寸,origin是切图坐标,该坐标是相对于图片左上角默认为(0,0)的相对像素坐标,anchor是锚点坐标,描述经纬度点对应图标中的位置
            var anchorb = new qq.maps.Point(3, -30),
                sizeb = new qq.maps.Size(42, 11),
                origin = new qq.maps.Point(0, 0),
                iconb = new qq.maps.MarkerImage(
                    "https://open.map.qq.com/doc/img/nilb.png",
                    sizeb,
                    origin,
                    anchorb
                );
            marker.setShadow(iconb);
            //设置标注的名称,当鼠标划过Marker时显示
            marker.setTitle("测试");
 
            //添加信息窗口
            var info = new qq.maps.InfoWindow({
                map: map
            });
            //获取标记的可拖动属性
            info.open();
            info.setContent('标记的可拖动属性为:' + marker.getDraggable());
            info.setPosition(marker.getPosition());
            //标记Marker点击事件
            qq.maps.event.addListener(marker, 'click', function() {
                info.open();
                info.setContent('<div style="text-align:center;white-space:nowrap;' +
                    'margin:10px;">单击标记</div>');
                info.setPosition(marker.getPosition());
            });
            //设置Marker停止拖动事件
            qq.maps.event.addListener(marker, 'dragend', function() {
                info.open();
                info.setContent('<div style="text-align:center;white-space:nowrap;' +
                    'margin:10px;">拖动标记</div>');
                //getPosition()  返回Marker的位置
                info.setPosition(marker.getPosition());
                alert(marker.getPosition())
            });
 
        }
    </script>
    <div style="width:100%;height:700px" id="container"></div>
    <p>Marker可以拖动,点击时弹出单击信息窗口,拖动Marker时弹出拖动信息窗口</p>
 
</body>
 
</html>

可输入地址查询的地图

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<iframe id="mapPage" width="100%" height="550px" frameborder=0
    src="http://apis.map.qq.com/tools/locpicker?search=1&type=1&key=M7VBZ-476RJ-TSIFB-FMZIF-YG4OT-RFFNG&referer=myapp">
</iframe>
	</body>
	<script>
    window.addEventListener('message', function(event) {
        // 接收位置信息,用户选择确认位置点后选点组件会触发该事件,回传用户的位置信息
        var loc = event.data;
        if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息,需判断module是否为'locationPicker'
          console.log('location', loc);
        }
    }, false);
</script>
</html>

	

最简单到的地图示例

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>简单地图</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <style type="text/css">
        html,body{
            width:100%;
            height:100%;
        }
        *{
            margin:0px;
            padding:0px;
        }
        body, button, input, select, textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        p{
            width:603px;
            padding-top:3px;
            overflow:hidden;
        }
        .btn{
            width:142px;
        }
        #container{
            min-width:600px;
            min-height:767px;
        }
    </style>
    <script charset="utf-8" src="http://map.qq.com/api/js?v=2.exp"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script>
 
 
        window.onload = function() {
            //初始化地图函数  自定义函数名init
            function init() {
                //定义map变量 调用 qq.maps.Map() 构造函数   获取地图显示容器
                var map = new qq.maps.Map(document.getElementById("container"), {
                    center: new qq.maps.LatLng(39.916527, 116.397128),      // 地图的中心地理坐标。
                    zoom: 8                                                 // 地图的中心地理坐标。
                });
                //添加监听事件
                qq.maps.event.addListener(map, 'click', function (e) {
                    var wei = e.latLng.getLat().toFixed(5);
                    var jing = e.latLng.getLng().toFixed(5);
                    var data = {
                        location: wei+','+jing,
                        key: "M7VBZ-476RJ-TSIFB-FMZIF-YG4OT-RFFNG", //key为自己向腾讯地图申请的密钥
                        get_poi: 0
                    };
                    var url = "http://apis.map.qq.com/ws/geocoder/v1/?";
                    data.output = "jsonp";
                    $.ajax({
                        type: "get",
                        dataType: 'jsonp',
                        data: data,
                        jsonp: "callback",
                        jsonpCallback: "QQmap",
                        url: url,
                        success: function (res) {
                            var add_info = res;
                            //console.log(add_info);
                            alert('纬度' + wei + ',经度' + jing+',省份'+add_info.result.address_component.province+',城市'+add_info.result.address_component.city);
                            //console.log(add_info.address_component.province,);
                        },
                        error: function (err) {
                            alert("服务端错误,请刷新浏览器后重试")
                        }
                    });
                });
            }
            init();//调用地图初始化函数
        }
    </script>
</head>
<body>
<!--   显示地图的区域   -->
<div id="container"></div>
</body>
</html>
 
 
 

 

### 如何在 Spring Boot 项目中使用腾讯地图 API 要在 Spring Boot 项目中集成并使用腾讯地图 API,主要涉及以下几个方面: #### 准备工作 首先需要注册成为开发者,并获取应用的密钥(Key)。这一步骤对于访问任何第三方服务都是必要的。完成此操作后,在项目的 `application.yml` 或者 `application.properties` 文件里配置好这些参数。 #### 添加依赖项 为了简化 HTTP 请求处理过程以及更好地管理 JSON 数据交换,建议引入像 RestTemplate 这样的工具类库。可以通过修改 Maven 的 POM 文件或 Gradle 构建脚本来添加相应的依赖包[^1]。 ```xml <!-- 对于Maven用户 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 如果还需要JSON支持可选加入Jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> ``` #### 创建服务层接口 定义一个用于封装调用腾讯地图 Web Service API 功能的服务组件。这里以地理编码为例展示基本框架结构[^4]: ```java import org.springframework.stereotype.Service; import java.io.IOException; @Service public class TencentMapService { private static final String GEOCODE_URL = "https://apis.map.qq.com/ws/geocoder/v1/"; /** * 获取地址对应的经纬度坐标. */ public Coordinate getCoordinateByAddress(String address, String key) throws IOException { // 组织请求URL... StringBuilder urlBuilder = new StringBuilder(GEOCODE_URL); urlBuilder.append("?address=").append(address).append("&key=").append(key); // 发送GET请求并解析响应数据... return null; // 返回结果对象 } } ``` 请注意上述代码片段仅作为示意用途,实际开发过程中还需考虑异常捕获、超时设置等因素;另外就是具体到不同类型的API调用时,传参方式也会有所区别,请参照官方文档说明进行调整。 #### 控制器实现 最后编写 RESTful 风格控制器以便外部能够触发该功能逻辑: ```java @RestController @RequestMapping("/tencentmap") public class TencentMapController { @Autowired private TencentMapService tencentMapService; @GetMapping("/geocode") public ResponseEntity<?> geocode(@RequestParam String address,@RequestParam String key){ try{ var coordinate=tencentMapService.getCoordinateByAddress(address,key); return ResponseEntity.ok(coordinate); }catch (Exception e){ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } } } ``` 以上即是在 Spring Boot 应用程序内接入腾讯地图开放平台所提供的部分基础服务的大致流程概述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值