首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Native Map -从服务器获取标记

React Native Map -从服务器获取标记
EN

Stack Overflow用户
提问于 2018-04-19 01:08:13
回答 1查看 929关注 0票数 1

我试图动态地从服务器获取附近的标记,并在地图上打印,但没有成功。当我更新地图位置时,我得到以下警告。当我拖动地图时,预期的行为是在地图上打印标记,但它不显示任何动态获取的标记,而是显示警告。

20:27:15:[未处理的promise rejection: objectkey:未定义不是对象(计算‘node_modules/immutability-helper/index.js:81:44’)]-node_modules/immutability-helper/index.js:81:44 in - node_modules/immutability-helper/index.js:73:29 in TypeError- ...来自框架内部的16个堆栈帧

App.js (主文件)

代码语言:javascript
复制
import React, { Component } from 'react';
import { Text, View, StyleSheet, Switch, Alert, AppRegistry } from 'react-native';
import MapView from 'react-native-maps';
import Fetchdata from './Fetchdata.js';

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
    height: 400,
    width: 400,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  map: {
    ...StyleSheet.absoluteFillObject,
  },
});

export default class myScreen extends Component {
  render() {
    return (
      <View style ={styles.container}>
        <Fetchdata />
      </View>
    );
  }
} 

Fetchdata.js (获取服务器数据的文件)

代码语言:javascript
复制
import React, { Component } from 'react'
import { Text, View, StyleSheet, Switch, Alert, AppRegistry} from 'react-native'
import MapView, {Marker, ProviderPropType} from 'react-native-maps';
import update from 'immutability-helper';

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
    height: 400,
    width: 400,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  map: {
    ...StyleSheet.absoluteFillObject,
  },
});
export default class Fetchdata extends Component {
  constructor (props) {
    super(props);
  };

  state = {
    latitude: 40.3565,
    longitude: 27.9774,
    markers: [
      {
        key: 1,
        latlng: {
          latitude: 40.3565,
          longitude: 27.9774
        }
      }
    ]
  };
  componentDidMount = () => {
    navigator.geolocation.getCurrentPosition(
        (position) => {
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            error: null,
      });
    },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
    );
   }
   onRegionChange (region) {
       fetch('https://isg.info.tr/query_maps.php' + '?latitude=' + region.latitude + '&longitude=' + region.longitude , {method: 'GET'})
        .then((response) => response.json())
        .then((responseJson) => {
          var newlatlng;
          for (i = 0 ; i< responseJson.length; i++) {
            newlatlng = update(this.state, {markers: { 
                                                        key: { $set: i }, 
                                                        latlng: { latitude: { $set: responseJson.latitude },
                                                        longitude: { $set:  responseJson.longitude  } 
                                                      } 
                                            }        
                                          });
            this.setState({markers});
          }
        })
   };
   render() {
      return (
        <View style={styles.container}>
          <MapView
                style={styles.map}
                initialRegion={{
                latitude: this.state.latitude,
                longitude: this.state.longitude,
                latitudeDelta: 0.015,
                longitudeDelta: 0.015,
            }}
            onRegionChange={this.onRegionChange.bind(this)}
            >
            {this.state.markers.map((marker, index) => (
              <MapView.Marker key={index} coordinate={marker.latlng} title={'marker.title'} />
            ))}
          </MapView>
      </View>
      );
   }
}
Fetchdata.propTypes = {
  provider: ProviderPropType,
};

从服务器获取的Jsondata:

代码语言:javascript
复制
[{"latlng":{"latitude":"40.3565","longitude":"27.9774"}},{"latlng":{"latitude":"40.3471","longitude":"27.9598"}},{"latlng":{"latitude":"40","longitude":"27.9708"}}]

EN

回答 1

Stack Overflow用户

发布于 2018-04-19 05:36:04

我不确定update函数,但在这里您将分配未定义的值:

延迟:{纬度:{ $set: responseJson.latitude },经度:{ $set: responseJson.longitude }

responseJson没有作为子级的纬度和经度。他们有LatLng

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49905341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档