openlayers在线地图:高德地图、天地图、谷歌、geoq(智图)

-----------------发现个不错的系列 不睡觉的怪叔叔 开源GIS

---------------------------------------------------------------------------------------------------------------------------------------------------------

对,没有百度地图,百度地图单独再说。

将获取的瓦片通过继承ol.source.XYZ来实现。

首先献上各地图获取地址:

var mapUrl = {
    /****
     * 高德地图
     * lang可以通过zh_cn设置中文,en设置英文,size基本无作用,scl设置标注还是底图,scl=1代表注记,
     * scl=2代表底图(矢量或者影像),style设置影像和路网,style=6为影像图,
     * vec——街道底图
     * img——影像底图
     * roadLabel---路网+标注
     */
    "aMap-img": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}",
    "aMap-vec": "http://webrd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}",
    "aMap-roadLabel": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}",
    /***
    *高德新版地图*
    */
    "aMap-vec-a": "http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7", //为矢量图(含路网、含注记)

    "aMap-img-n": "http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=6", //为影像底图(不含路网,不含注记)

    "aMap-img-a": "http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=8", //为影像路图(含路网,含注记)

    /***
     * 天地图 要key的
     * vec——街道底图
     * img——影像底图
     * ter——地形底图
     * cva——中文注记
     * cta/cia——道路+中文注记 ---roadLabel
     */
    "tian-img": "http://t{0-7}.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=你的密钥",
    "tian-roadLabel": "http://t{0-7}.tianditu.gov.cn/DataServer?T=cta_w&x={x}&y={y}&l={z}&tk=你的密钥",
    "tian-label": "http://t{0-7}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=你的密钥",
    "tian-vec": "http://t{0-7}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=你的密钥",
    "tian-ter": "http://t{0-7}.tianditu.gov.cn/DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=你的密钥",
    /***
     *geoq地图
     * http://cache1.arcgisonline.cn
     * http://map.geoq.cn
     * vec:标准彩色地图
     * gray、blue、warm
     * line 中国轮廓图
     * china 中国轮廓图+标注
     * Hydro 水系
     * green 植被
     */
    "geoq-vec": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}",
    "geoq-gray": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}",
    "geoq-blue": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
    "geoq-warm": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetWarm/MapServer/tile/{z}/{y}/{x}",
    "geoq-line": "http://cache1.arcgisonline.cn/arcgis/rest/services/SimpleFeature/ChinaBoundaryLine/MapServer/tile/{z}/{y}/{x}",//不稳定
    "geoq-china": "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/administrative_division_boundaryandlabel/MapServer/tile/{z}/{y}/{x}",//不稳定
    "geoq-Hydro": "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/WorldHydroMap/MapServer/tile/{z}/{y}/{x}",//不稳定
    "geoq-green": "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/vegetation/MapServer/tile/{z}/{y}/{x}",//不稳定
    /***
     * Google
     * m 街道
     * s 影像
     */
    "google-vec": "http://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}",
    "google-img": "http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}"

};

 

继承xyz:

 

ol.source.onlineMap = function (options) {
    var options = options ? options : {};

    var attributions;//右下角标识
    if (options.attributions !== undefined) {
        attributions = option.attributions;
    } else if (options.mapType.indexOf("aMap") != -1) {
        attributions = new ol.Attribution({
            html: '&copy; <a class="ol-attribution-amap" ' + 'href="http://ditu.amap.com/">' + '高德地图</a>'
        });
    } else if (options.mapType.indexOf("tian") != -1) {
        attributions = new ol.Attribution({
            html: '&copy; <a class="ol-attribution-tianmap" ' + 'href="http://www.tianditu.cn/">' + '天地图</a>'
        });
    } else if (options.mapType.indexOf("geoq") != -1) {
        attributions = new ol.Attribution({
            html: '&copy; <a class="ol-attribution-geoqmap" ' + 'href="http://www.geoq.net/basemap.html">' + '智图地图</a>'
        });
    } else if (options.mapType.indexOf("google") != -1) {
        attributions = new ol.Attribution({
            html: '&copy; <a class="ol-attribution-googlemap" ' + 'href="http://www.google.cn/maps">' + '谷歌地图</a>'
        });
    }
    var url = mapUrl[options.mapType];
    ol.source.XYZ.call(this, {
        crossOrigin: 'anonymous',   //跨域
        cacheSize: options.cacheSize,
        projection: ol.proj.get('EPSG:3857'),
        url: url,
        attributions: attributions,
        wrapX: options.wrapX !== undefined ? options.wrapX : true
    });
};
ol.inherits(ol.source.onlineMap, ol.source.XYZ);//必需

食用方法:

以高德地图为例:

 var map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                title: "高德影像地图",
                visible:false,
                source: new ol.source.onlineMap({mapType:"aMap-img"})
            }),
            new ol.layer.Tile({
                title: "高德矢量图",
                source: new ol.source.onlineMap({mapType:"aMap-vec"})
            }),
            new ol.layer.Tile({
                title: "高德路网地图",
                visible:false,
                source: new ol.source.onlineMap({mapType:"aMap-roadLabel"})
            })
        ],
        target: 'map',
        view: new ol.View({
            center:center,
            zoom: 4
        })
    });

 

一眼就看出来了各地图的风格不同:

  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers是一个开源的JavaScript库,用于在Web页面上显示交互式地图高德地图是国内一家知名的地图服务提供商,OpenLayers可以通过加载高德地图的图层来显示高德地图数据。 在OpenLayers中加载高德地图时,需要使用高德地图的瓦片图层URL和投影方式。通过设置正确的投影方式,可以确保地图数据在OpenLayers中正确显示。在代码中,可以使用类似下面的方式加载高德地图图层: ```javascript var gdMapLayer = new TileLayer({ source: new XYZ({ projection: gcj02Mecator, url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}' }), zIndex: 0 }); ``` 这段代码中,`gcj02Mecator`是高德地图的投影方式,`url`是高德地图瓦片的URL模板。通过设置正确的URL和投影方式,可以加载高德地图的图层并在OpenLayers中显示。 然而,根据引用\[1\]和引用\[3\]的描述,切换到高德地图后可能会出现坐标点偏移的问题。这可能是由于高德地图和其他地图服务商使用不同的坐标系统导致的。在切换地图时,需要确保坐标点的转换和显示方式正确,以避免出现位置偏移的情况。 总结来说,OpenLayers可以通过加载高德地图的图层来显示高德地图数据。但在切换地图时,需要注意坐标点的转换和显示方式,以确保地图数据在不同地图之间正确显示。 #### 引用[.reference_title] - *1* *2* *3* [vue openlayers 加载高德地图等 gcj02 的图层偏移问题](https://blog.csdn.net/weixin_42776111/article/details/126539024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值