简体   繁体   English

无法读取未定义和未定义的属性“纬度”

[英]Cannot read property 'lat' of undefined and undefined

I'm now trying to feed the position of the user through the variable (coords) but every time I pass any variable into onClickUserLoc() the variable has the error我现在正在尝试通过变量(coords)输入用户的 position 但每次我将任何变量传递给 onClickUserLoc() 时,变量都会出现错误

Cannot read property 'lat' of undefined无法读取未定义的属性“纬度”

and when I console.log it states undefined?当我 console.log 它状态未定义? The coords variable holds an array of location data such as lng and lat but become undefined in onClickUserLoc(). coords 变量包含一个位置数据数组,例如 lng 和 lat,但在 onClickUserLoc() 中未定义。

Code:代码:

    export default class App extends React.Component {
          constructor() {
            super();
            this.state = {
               ready: false,
               where: { lat: '', lng: '' },
               error: null,
               };
           this.onClickUserLoc = this.onClickUserLoc.bind(this)
          }
          
    componentDidMount() {
    let geoOptions = {
      enableHighAccuracy: true,
      timeOut: 20000,
      maximumAge: 60 * 60 * 24,
    };
    this.setState({ ready: false, error: null });
    navigator.geolocation.getCurrentPosition(
      this.geoSuccess,
      this.geoFailure,
      geoOptions
    );
  }

  mapRef = React.createRef();

  

  geoSuccess = (position) => {
    console.log(position.coords.latitude);
    console.log(position.coords.longitude);
    console.log(this.state.where?.lng);
    console.log(this.state.where?.lat);
    

    this.setState({
      ready: true,
      where: { lat: position.coords.latitude, lng: position.coords.longitude 
      },
      
    });
    console.log(this.state.where?.lng);
    console.log(this.state.where?.lat);
  };
  geoFailure = (err) => {
    this.setState({ error: err.message });
    console.log(this.state.error);
  };

  

          onClickUserLoc({ coords }) {
            this.mapRef.current.leafletElement.flyTo(coords, 15);
            console.log(coords);
          }

         
render() {

const coords = [this.state.where?.lat, this.state.where?.lng];
        return (
            <>
            <Button onPress={this.onClickUserLoc}>
            <Map 
             center={[...]} 
             zoom={0}> 
             style={{ height: "90vh" }}
             ref={this.mapRef}
             
              <TileLayer
                  attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                  url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                />
             </map>
            </>
      )
    }

If I understand correctly you want to fly to the position you are right now (geolocation).如果我理解正确,您想飞往 position,您现在就是(地理位置)。 Variable coords variable is defined inside render method.变量coords变量在 render 方法中定义。 You either pass the coords variable as an argument to button's onPress:您可以将 coords 变量作为参数传递给按钮的 onPress:

 <Button onPress={() => this.onClickUserLoc(coords)}></Button>

but you don't need to destructure it here但你不需要在这里解构它

onClickUserLoc(coords) { // here no need to destructure it.
   this.mapRef.current.leafletElement.flyTo(coords, 15);
}

or use the state variable where directly inside onClickUserLoc without passing any argument:或直接where onClickUserLoc内部使用 state 变量而不传递任何参数:

 onClickUserLoc() {
    const {
      where: { lat, lng }
    } = this.state;
    this.mapRef.current.leafletElement.flyTo([lat, lng], 15);
  }

Demo 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “未捕获的TypeError:无法读取未定义的属性&#39;lat&#39;” - “Uncaught TypeError: Cannot read property 'lat' of undefined” JS-错误:无法读取未定义的属性“ lat” - JS - error: Cannot read property 'lat' of undefined 无法使用Google Maps读取未定义的属性“ lat” - Cannot read property 'lat' of undefined using google maps 使用地图框和传单获取“未捕获的类型错误:无法读取未定义的属性 &#39;lat&#39;” - Getting “Uncaught TypeError: Cannot read property 'lat' of undefined” with mapbox and leaflet 未捕获的TypeError:无法读取未定义的传单地图ArcIGS的属性“ lat” - Uncaught TypeError: Cannot read property 'lat' of undefined Leaflet map ArcIGS 未捕获的TypeError:无法读取未定义的Leaflet和VueJS的属性&#39;lat&#39; - Uncaught TypeError: Cannot read property 'lat' of undefined Leaflet and VueJS Mapbox / Leaflet Javascript:未捕获的TypeError:无法读取未定义的属性“ lat” - Mapbox/Leaflet Javascript : Uncaught TypeError: Cannot read property 'lat' of undefined 未捕获的类型错误:无法读取未定义 React.JS 的属性“lat” - Uncaught TypeError: Cannot read property 'lat' of undefined React.JS 无法读取未定义的属性“未定义” - Cannot read property 'undefined' of undefined 无法读取未定义的属性“结果” - var lat = data.results[0].geometry.location.lat; - Cannot read property 'results' of undefined - var lat = data.results[0].geometry.location.lat;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM