react-google-maps-api icon indicating copy to clipboard operation
react-google-maps-api copied to clipboard

KML Layer won't display

Open Grumpier opened this issue 2 years ago • 3 comments

Discussed in https://github.com/JustFly1984/react-google-maps-api/discussions/3163

Originally posted by Grumpier December 14, 2022 I've just started experimenting with react-google-maps-api and I cannot get a kml track to display in a map. The url I am using works fine if I plug it into the KML Layer example on netify. I can get circles to display in the map without a problem. Here's my component:

Screenshot 2022-12-14 at 18 56 16

Here's my screen with a log of the KmlLayer object:

Screenshot 2022-12-14 at 18 44 02

You can see that the component is mounted with a status of "ok".

Does anyone have an idea of what might be my problem?

Thanks!

Grumpier avatar Dec 15 '22 19:12 Grumpier

Is you kmlUrl value accessible to the public? If that URL points to anything behind a firewall or authentication layer it will not load. I've run into this issue myself.

gkrinc avatar Dec 16 '22 18:12 gkrinc

Thanks gkrinc. I don't think that's the issue since you can see that when I log the call the status is "OK" and metadata is being returned. Also, when I use that url in the style guide example, it appears fine.

image

Grumpier avatar Dec 16 '22 19:12 Grumpier

On my side, what I finally did was something like this :

import { GoogleMap, LoadScriptNext } from '@react-google-maps/api';
import React, { useRef } from 'react';
import Spinner from '../Spinner';

interface MapProps {
  width?: number;
  height?: number;
  center?: google.maps.LatLng | google.maps.LatLngLiteral;
  zoom?: number;
  apiKey: string;
  kmlUrl?: string;
}

const center = {
  lat: 48.5,
  lng: 2.2,
};

function Map({ width = 800, height = 600, zoom = 10, apiKey, kmlUrl }: MapProps) {
  function onMapLoad(map: google.maps.Map) {
    if (kmlUrl) {
      new google.maps.KmlLayer({
        url: kmlUrl,
        preserveViewport: true,
        suppressInfoWindows: true,
        map,
      });
    }
  }

  return (
    <LoadScriptNext googleMapsApiKey={apiKey} loadingElement={<Spinner />}>
      <GoogleMap
        id="kml-layer-example"
        mapContainerStyle={{
          height: `${height}px`,
          width: `${width}px`,
        }}
        zoom={zoom}
        center={center}
        onLoad={onMapLoad}
      />
    </LoadScriptNext>
  );
}

export default React.memo(Map);

florianchevallier avatar May 31 '23 15:05 florianchevallier