Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.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
Reactjs 地理编码API-地址不更新为lat long,而是作为邮政编码文本持久化到数据库_Reactjs_Geocode - Fatal编程技术网

Reactjs 地理编码API-地址不更新为lat long,而是作为邮政编码文本持久化到数据库

Reactjs 地理编码API-地址不更新为lat long,而是作为邮政编码文本持久化到数据库,reactjs,geocode,Reactjs,Geocode,我正在尝试使用谷歌地理编码api将邮政编码转换为lat-long坐标 当我输入console.log.state时,它似乎正在工作,但是邮政编码文本即“TW135QZ”将被持久化到数据库中,而不是lat long坐标 有人知道为什么lat长跳线没有被持久化吗?我现在卡住了 主要代码位: onCreateCat = (e, authUser) => { Geocode.fromAddress(this.state.address).then( response =&g

我正在尝试使用谷歌地理编码api将邮政编码转换为lat-long坐标

当我输入console.log.state时,它似乎正在工作,但是邮政编码文本即“TW135QZ”将被持久化到数据库中,而不是lat long坐标

有人知道为什么lat长跳线没有被持久化吗?我现在卡住了

主要代码位:

  onCreateCat = (e, authUser) => {
    Geocode.fromAddress(this.state.address).then(
      response => {
        const { lat, lng } = response.results[0].geometry.location;
        this.setState({address: lat + ',' + lng});
      },
      error => {
        console.error(error);
      }
    )
    .then(
      this.props.firebase.cats().push({
        text: this.state.text,
        image: 'https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9',
        userId: authUser.uid,
        address: this.state.address,
      })
    )
    e.preventDefault();
  }
完整代码:

const INITIAL_STATE = {
  text: '',
  address: ''
}

class AddCat extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      ...INITIAL_STATE
    }
  }

  componentDidMount() {
    // set Google Maps Geocoding API for purposes of quota management. Its optional but recommended.
    Geocode.setApiKey(process.env.REACT_APP_GOOGLEKEY);
    // set response language. Defaults to english.
    Geocode.setLanguage("en");
    // set response region. Its optional.
    // A Geocoding request with region=es (Spain) will return the Spanish city.
    Geocode.setRegion("es");
    // Enable or disable logs. Its optional.
    Geocode.enableDebug();
    // Get latidude & longitude from address.
  }

  onCreateCat = (e, authUser) => {
    Geocode.fromAddress(this.state.address).then(
      response => {
        const { lat, lng } = response.results[0].geometry.location;
        this.setState({address: lat + ',' + lng});
      },
      error => {
        console.error(error);
      }
      ).then(
      this.props.firebase.cats().push({
        text: this.state.text,
        image: 'https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9',
        userId: authUser.uid,
        address: this.state.address,
      })
    )
    e.preventDefault();
  }

  onChangeText = e => {
    this.setState({ text: e.target.value });
  };

  onChangeAddress = e => {
    this.setState({ address: e.target.value });
  };

  render() {

    const { text, address } = this.state;

    console.log(this.state);

    return (
      <div>
      <h1>Add cat</h1>
      <AuthUserContext.Consumer>
        {authUser => (
          <div>
            <form onSubmit={e => this.onCreateCat(e, authUser)}>
              <input
                type="text"
                value={text}
                onChange={this.onChangeText}
                placeholder="Cats Name"
              />
              <input
                name="address"
                value={address}
                onChange={this.onChangeAddress}
                type="text"
                placeholder="Cats Postcode">
              </input>
              <button type="submit">Send</button>
            </form>
          </div>
        )}
       </AuthUserContext.Consumer>
       </div>
    );

  }

}

const condition = authUser => !!authUser;

export default withAuthorization(condition)(AddCat);
const初始状态={
文本:“”,
地址:''
}
类AddCat扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
…初始状态
}
}
componentDidMount(){
//设置GoogleMaps地理编码API用于配额管理。可选但推荐。
Geocode.setApiKey(process.env.REACT\u APP\u GOOGLEKEY);
//设置响应语言。默认为英语。
地理编码。设置语言(“en”);
//设置响应区域。它是可选的。
//region=es(西班牙)的地理编码请求将返回西班牙城市。
地理编码。setRegion(“es”);
//启用或禁用日志。它是可选的。
Geocode.enableDebug();
//从地址获取纬度和经度。
}
onCreateCat=(e,authUser)=>{
地理编码.fromAddress(this.state.address)。然后(
响应=>{
const{lat,lng}=response.results[0].geometry.location;
this.setState({地址:lat+','+lng});
},
错误=>{
控制台错误(error);
}
).那么(
这个.props.firebase.cats().push({
text:this.state.text,
图像:'https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9',
userId:authUser.uid,
地址:this.state.address,
})
)
e、 预防默认值();
}
onChangeText=e=>{
this.setState({text:e.target.value});
};
onChangeAddress=e=>{
this.setState({地址:e.target.value});
};
render(){
const{text,address}=this.state;
console.log(this.state);
返回(
加猫
{authUser=>(
this.onCreateCat(e,authUser)}>
发送
)}
);
}
}
const condition=authUser=>!!authUser;
使用授权(条件)导出默认值(AddCat);

首先保存坐标,然后更新状态。检查主要代码位,在将cat对象推入数据库之前,我正在更新地址的状态