React-TypeScript: Property 'position' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<GoogleMapReact>

只愿长相守 提交于 2020-05-17 08:50:09

问题


I am learning React-typescript. I am using react google maps to show my area. I successfully able to display the map. When I tried to use Marker from react-google-map to point out my area and positioned my latitude and longitude. I am getting typescript error: Property 'position' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<GoogleMapReact> & Readonly<Props> & Readonly<...>. I really don't know how to fix it.

import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import ErrorBoundary from 'components/errorBoundary';
import GoogleMapReact from 'google-map-react';
import Marker from 'google-map-react'

export interface ITestNewListProps {
  className?: string;
  position?: { lat: number; lng: number; };
}

const TestMaps = ({ className, position }: ITestNewListProps) => {

  const [state, setstate] = useState(
    {
      center: {
        lat: 60.1098678,
        lng: 24.7385084
      },
      zoom: 7
    }
  )

  return (
    <ErrorBoundary id="TestNewListErrorBoundary">
      <div className={`${className}`}>
        <div style={{ height: '100vh', width: '100%' }}>
          <GoogleMapReact
            bootstrapURLKeys={{ key: "*************************" }}
            defaultCenter={state.center}
            defaultZoom={state.zoom}
          >
            <Marker position={{ lat: 60.1098678, lng: 24.7385084 }} />//Geting error from here
          </GoogleMapReact>
        </div>
      </div>

    </ErrorBoundary>
  );
};

TestMaps.displayName = `test`;

export default styled(TestMaps)`
`;

回答1:


Remember that google-map-react allows you to render any React component on the map.
The library doesn't export a Marker component.
However, if you want to use the default Google Maps markers you can see a solution here.
Based on the example from the documentation, you should be able to achieve something like this by defining your own Marker component:

import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';

const Marker = ({ text }) => <div>{text}</div>;

class SimpleMap extends Component {
  static defaultProps = {
    center: {
      lat: 59.95,
      lng: 30.33
    },
    zoom: 11
  };

  render() {
    return (
      <div style={{ height: '100vh', width: '100%' }}>
        <GoogleMapReact
          bootstrapURLKeys={{ key: /* YOUR KEY HERE */ }}
          defaultCenter={this.props.center}
          defaultZoom={this.props.zoom}
        >
          <Marker
            lat={59.955413}
            lng={30.337844}
            text="My Marker"
          />
        </GoogleMapReact>
      </div>
    );
  }
}

export default SimpleMap;


来源:https://stackoverflow.com/questions/61679036/react-typescript-property-position-does-not-exist-on-type-intrinsicattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!