Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';纬度';指未定义的和未定义的_Javascript_Reactjs_Leaflet_React Leaflet - Fatal编程技术网

Javascript 无法读取属性';纬度';指未定义的和未定义的

Javascript 无法读取属性';纬度';指未定义的和未定义的,javascript,reactjs,leaflet,react-leaflet,Javascript,Reactjs,Leaflet,React Leaflet,我现在正试图通过变量(coords)来输入用户的位置,但每次我将任何变量传递到onClickUserLoc()时,该变量都会出错 无法读取未定义的属性“lat” 当I console.log时,它表示未定义?coords变量保存位置数据数组,如lng和lat,但在onClickUserLoc()中未定义 代码: 导出默认类App扩展React.Component{ 构造函数(){ 超级(); 此.state={ 就绪:错误, 其中:{lat:'',lng:'}, 错误:null, }; this

我现在正试图通过变量
(coords)
来输入用户的位置,但每次我将任何变量传递到onClickUserLoc()时,该变量都会出错

无法读取未定义的属性“lat”

当I console.log时,它表示未定义?coords变量保存位置数据数组,如lng和lat,但在onClickUserLoc()中未定义

代码:

导出默认类App扩展React.Component{
构造函数(){
超级();
此.state={
就绪:错误,
其中:{lat:'',lng:'},
错误:null,
};
this.onClickUserLoc=this.onClickUserLoc.bind(this)
}
componentDidMount(){
让geoOptions={
EnableHighAccurance:正确,
超时:20000,
最大尺寸:60*60*24,
};
this.setState({ready:false,error:null});
navigator.geolocation.getCurrentPosition(
这是一次成功,
这是一次失败,
地理选项
);
}
mapRef=React.createRef();
地理成功=(位置)=>{
控制台.日志(位置.坐标.纬度);
console.log(位置坐标经度);
console.log(此.state.where?.lng);
console.log(this.state.where?.lat);
这是我的国家({
准备好了吗,
其中:{lat:position.coords.lation,lng:position.coords.longitude
},
});
console.log(此.state.where?.lng);
console.log(this.state.where?.lat);
};
地理故障=(错误)=>{
this.setState({error:err.message});
console.log(this.state.error);
};
onClickUserLoc({coords}){
this.mapRef.current.mopleElement.flyTo(coords,15);
控制台日志(coords);
}
render(){
const coords=[this.state.where?.lat,this.state.where?.lng];
返回(
样式={height:“90vh”}
ref={this.mapRef}
)
}

如果我理解正确,您希望飞到当前位置(地理位置)。变量
coords
变量在render方法中定义。您可以将coords变量作为参数传递给button的onPress:

 <Button onPress={() => this.onClickUserLoc(coords)}></Button>
或者直接在
onClickUserLoc
中使用状态变量
where
,而不传递任何参数:

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

您的
coords
变量来自何处?它是在何处定义的?您需要将它作为参数传递给
onClickUserLoc
,如果它在调用itI之前未定义,我正在创建常量(coords)将用户的lat和long数据从渲染中的this.state组合在一起。OnClickUserLoc和const(coords)之后渲染的问题是否只影响渲染中的内容,例如贴图?
onClickUserLoc(coords) { // here no need to destructure it.
   this.mapRef.current.leafletElement.flyTo(coords, 15);
}
 onClickUserLoc() {
    const {
      where: { lat, lng }
    } = this.state;
    this.mapRef.current.leafletElement.flyTo([lat, lng], 15);
  }