openlayers3学习 使用天地地图画世界

基础的东西就不说了,以下只是个人学习成功记录,对你有帮助过更好,有错误也可以指出,互相学习。

准备工作

安装依赖…配置环境什么的就都不说了

需要使用的网站:
ol3示例:点击跳转ol3示例 仅供参考
ol3官方:点击跳转ol3官方 英文文档,看不懂推荐edge浏览器翻译

1.画底图

import React, { useEffect, useRef, useState } from 'react';
import Map from 'ol/Map.js';
import { XYZ } from 'ol/source';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import 'ol/ol.css';

export default () => {
  const containerRef = useRef();

  useEffect(() => {
    const map = new Map({
      /**
       * T=vec_c 经纬度投影
       * T=vec_w 球面墨卡托投影
       */
      layers: [
      // 新建图层不用说了吧
        new TileLayer({
          source: new XYZ({
            // url: 'http://t2.tianditu.com/DataServer?T=vec_c&x={x}&y={y}&l={z}&tk=3fef19ca23f6e81f07a697cff1f775f5',
            url: 'http://t0.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=3fef19ca23f6e81f07a697cff1f775f5',
          }),
        }),
      ],
      target: containerRef.current, // 地图渲染的dom元素,可直接传递id
      view: new View({
        // ol默认的经纬度格式是-EPSG:3857,需改为日常使用的EPSG:4326
        projection: 'EPSG:4326',
        // center: [116.40969, 39.89945],
        center: [115.25965131, 39.2636281],
        zoom: 5,
        // minZoom: 5,
      }),
    });

  }, []);



  return ( 
      <div
        style={{ width: 1500, height: 550, border: '1px solid' }}
        ref={containerRef}
        className="map"
      />  
  );
};

效果图:
在这里插入图片描述

2.画底图(卫星)

import React, { useEffect, useRef, useState } from 'react';
import Map from 'ol/Map.js';
import { XYZ } from 'ol/source';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import 'ol/ol.css';

export default () => {
  const containerRef = useRef();

  useEffect(() => {
    const map = new Map({
      /**
       * T=vec_c 经纬度投影
       * T=vec_w 球面墨卡托投影
       */
      layers: [
        // 卫星影像
        new TileLayer({
          source: new XYZ({
            url: 'http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=3fef19ca23f6e81f07a697cff1f775f5',
          }),
          // name: 'BaseMap',
        }),
      ],
      target: containerRef.current, // 地图渲染的dom元素,可直接传递id
      view: new View({
        // ol默认的经纬度格式是-EPSG:3857,需改为日常使用的EPSG:4326
        projection: 'EPSG:4326',
        // center: [116.40969, 39.89945],
        center: [115.25965131, 39.2636281],
        zoom: 5,
        // minZoom: 5,
      }),
    });

  }, []);



  return (
    <div
      style={{ width: 1500, height: 550, border: '1px solid' }}
      ref={containerRef}
      className="map"
    />
  );
};


示例:
在这里插入图片描述

添加位置标注图层

import React, { useEffect, useRef, useState } from 'react';
import Map from 'ol/Map.js';
import { XYZ } from 'ol/source';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import 'ol/ol.css';

export default () => {
  const containerRef = useRef();

  useEffect(() => {
    const map = new Map({
      /**
       * T=vec_c 经纬度投影
       * T=vec_w 球面墨卡托投影
       */
      layers: [
        new TileLayer({
          source: new XYZ({
            // url: 'http://t2.tianditu.com/DataServer?T=vec_c&x={x}&y={y}&l={z}&tk=3fef19ca23f6e81f07a697cff1f775f5',
            url: 'http://t0.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=3fef19ca23f6e81f07a697cff1f775f5',
          }),
        }),
        // 显示地名
        new TileLayer({
          source: new XYZ({
            url: 'http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=3fef19ca23f6e81f07a697cff1f775f5',
          }),
        }),

      ],
      target: containerRef.current, // 地图渲染的dom元素,可直接传递id
      view: new View({
        // ol默认的经纬度格式是-EPSG:3857,需改为日常使用的EPSG:4326
        projection: 'EPSG:4326',
        // center: [116.40969, 39.89945],
        center: [115.25965131, 39.2636281],
        zoom: 5,
        // minZoom: 5,
      }),
    });

  }, []);



  return (
    <div
      style={{ width: 1500, height: 550, border: '1px solid' }}
      ref={containerRef}
      className="map"
    />
  );
};

示例:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值