尝试创建一个基于React的地图应用程序

起初

我是在HERE工作的kekishida。
之前,在我的文章中介绍了将HERE Maps API for Javascript转化为React组件的示例。

 

image.png
    • HERE Maps API for Javascript

 

    HERE Geocoding & Search

 

我们就开始吧。

所需之物

以下是样本代码的简介,需要获取HERE账户。

请注册HERE账号

 

制作过程 (zhì zuò guò

我将逐步介绍以下代码。除了APIKEY之外,基本上都可以通过复制粘贴来实现。

创建一个React项目

npx create-react-app <プロジェクトフォルダ名>

编辑index.html

请将index.html按照以下方式进行修改。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>HERE Maps API for Javascript Demo</title>
    <meta name="viewport" content="initial-scale=1.0, width=device-width" />
    <!-- <link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" /> -->
    <script
      type="text/javascript"
      src="https://js.api.here.com/v3/3.1/mapsjs-core.js"
    ></script>
    <script
      type="text/javascript"
      src="https://js.api.here.com/v3/3.1/mapsjs-service.js"
    ></script>
    <script
      type="text/javascript"
      src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"
    ></script>
    <script
      type="text/javascript"
      src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"
    ></script> 
    <script 
      type="text/javascript" 
      src="https://js.api.here.com/v3/3.1/mapsjs-clustering.js"
    ></script> 
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

编辑index.js

请将index.js按照以下方式改写。

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <App/>
);


编辑App.js

请将App.js如下修改。请将以下APIKEY替换为获取到的APIKEY。

import Map from './Map';
import Search from './Search';
import { useState } from 'react';
let apikey = <APIKEY>;

function App() {
    const [ gps, setGps ] = useState({lat: "35.6814568602531", lng: "139.76799772026422"});
    return (
    <div>
      <Search apikey={apikey} onGeocodeResult={query=> setGps(query)}/>
      <Map apikey={apikey} gps={gps}/>
    </div>
  );
};

export default App;

创建Map.js

请添加以下文件。

import * as React from 'react';

const Map = (props) => {
  const apikey = props.apikey;
  const gps = props.gps;
  // Create a reference to the HTML element we want to put the map on
  const mapRef = React.useRef(null);
  /**
   * Create the map instance
   * While `useEffect` could also be used here, `useLayoutEffect` will render
   * the map sooner
   */
    // React.useEffect(() => {
   React.useLayoutEffect(() => {
    // `mapRef.current` will be `undefined` when this hook first runs; edge case that
    if (!mapRef.current) return;
    const H = window.H;
    const platform = new H.service.Platform({
        apikey: apikey
    });
    const defaultLayers = platform.createDefaultLayers();
    // configure an OMV service to use the `core` endpoint
    var omvService = platform.getOMVService({ path: "v2/vectortiles/core/mc" });
    var baseUrl = "https://js.api.here.com/v3/3.1/styles/omv/oslo/japan/";

    // create a Japan specific style
    var style = new H.map.Style(`${baseUrl}normal.day.yaml`, baseUrl);

    // instantiate provider and layer for the base map
    var omvProvider = new H.service.omv.Provider(omvService, style);
    var omvlayer = new H.map.layer.TileLayer(omvProvider, { max: 22 ,dark:true});

    // instantiate (and display) a map:
    var map = new H.Map(mapRef.current, omvlayer, {
        zoom: 16,
        center: { lat: gps.lat, lng: gps.lng },
    });

    // add a resize listener to make sure that the map occupies the whole container
    window.addEventListener("resize", () => map.getViewPort().resize());

    // MapEvents enables the event system
    // Behavior implements default interactions for pan/zoom (also on mobile touch environments)
    var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
    // Create the default UI components
    var ui = H.ui.UI.createDefault(map, defaultLayers); 
    // Marker code goes here
    var LocationOfMarker = { lat: gps.lat, lng: gps.lng };
    var pngIcon = new H.map.Icon('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M12 2c2.131 0 4 1.73 4 3.702 0 2.05-1.714 4.941-4 8.561-2.286-3.62-4-6.511-4-8.561 0-1.972 1.869-3.702 4-3.702zm0-2c-3.148 0-6 2.553-6 5.702 0 3.148 2.602 6.907 6 12.298 3.398-5.391 6-9.15 6-12.298 0-3.149-2.851-5.702-6-5.702zm0 8c-1.105 0-2-.895-2-2s.895-2 2-2 2 .895 2 2-.895 2-2 2zm10.881-2.501c0-1.492-.739-2.83-1.902-3.748l.741-.752c1.395 1.101 2.28 2.706 2.28 4.5s-.885 3.4-2.28 4.501l-.741-.753c1.163-.917 1.902-2.256 1.902-3.748zm-3.381 2.249l.74.751c.931-.733 1.521-1.804 1.521-3 0-1.195-.59-2.267-1.521-3l-.74.751c.697.551 1.141 1.354 1.141 2.249s-.444 1.699-1.141 2.249zm-16.479 1.499l-.741.753c-1.395-1.101-2.28-2.707-2.28-4.501s.885-3.399 2.28-4.5l.741.752c-1.163.918-1.902 2.256-1.902 3.748s.739 2.831 1.902 3.748zm.338-3.748c0-.896.443-1.698 1.141-2.249l-.74-.751c-.931.733-1.521 1.805-1.521 3 0 1.196.59 2.267 1.521 3l.74-.751c-.697-.55-1.141-1.353-1.141-2.249zm16.641 14.501c0 2.209-3.581 4-8 4s-8-1.791-8-4c0-1.602 1.888-2.98 4.608-3.619l1.154 1.824c-.401.068-.806.135-1.178.242-3.312.949-3.453 2.109-.021 3.102 2.088.603 4.777.605 6.874-.001 3.619-1.047 3.164-2.275-.268-3.167-.296-.077-.621-.118-.936-.171l1.156-1.828c2.723.638 4.611 2.016 4.611 3.618z"/></svg>', { size: { w: 56, h: 56 } });

    // Create a marker using the previously instantiated icon:
    var marker = new H.map.Marker(LocationOfMarker, { icon: pngIcon });

    // Add the marker to the map:
    map.addObject(marker);
   
    // Optionally, 
    //Show the marker in the center of the map
    map.setCenter(LocationOfMarker)

    // This will act as a cleanup to run once this hook runs again.
    // This includes when the component un-mounts
    return () => {
      map.dispose();
    };
  }, [props.gps]); // This will run this hook every time this ref is updated
  return <div style={ { width: "100%", height: "500px" } }  ref={mapRef}  />;
};
export default Map;

创建Search.js

请将以下文件添加进去。

import * as React from 'react';
import { useState } from 'react';

const Search = (props) => {
  const [text,setText] = useState("");
  const [addText,setAddText] = useState("");

  const apikey = props.apikey;
  const H = window.H;
  const platform = new H.service.Platform({
      apikey: apikey
  });
  const service = platform.getSearchService();
  const onClickSearch = () => {
      service.geocode({
        q: text,
        limit: 1
      }, (result) => {
        result.items.forEach((item) => {
          console.log(item.position);
          props.onGeocodeResult(item.position);
        });
      }, alert);
    setText("");
  }


  return (
    <div>
        <input type="text" name="query" id="query" value={text}
          onChange={(event)=>setText(event.target.value)}
          placeholder="Search location" />
        <button onClick={onClickSearch}>Search</button>
    </div>
  );
};
export default Search;
image.png

项目的执行 de

npm start
完成イメージ.gif

最后

这次介绍了将HERE Maps API for Javascript进行React组件化的示例。虽然在以前的文章中也进行了介绍,但是这次介绍的代码可以基于此,不仅可以实现HERE Geocoding & Search API,还可以容易地实现类似聚类功能和HERE Routing API等。希望可以作为参考。非常感谢您的阅读。

 

bannerAds