Javascript 如何放大设置的最后一个标记?
我让一切正常,但似乎找不到正确的代码来放大最后一个标记 这是我的代码:Javascript 如何放大设置的最后一个标记?,javascript,android,react-native,Javascript,Android,React Native,我让一切正常,但似乎找不到正确的代码来放大最后一个标记 这是我的代码: getEarthquakes(){ return fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson') .then(response => response.json()) .then(responseData =>{ var markers =
getEarthquakes(){
return fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson')
.then(response => response.json())
.then(responseData =>{
var markers = [];
for (var i = 0; i < responseData.features.length; i++) {
var coords = responseData.features[i].geometry.coordinates;
var marker = {
key: responseData.features[i].id,
coordinate: {
latitude: coords[1],
longitude: coords[0],
}
}
markers.push(marker);
}
this.setState({
markers: markers,
loaded: true,
marker: markers,
latitude: coords[1],
longitude: coords[0]
});
}
).done();
}
render() {
return (
<View style={styles.container}>
<MapView.Animated
style={styles.map}
region={this.state.marker}
showsUserLocation={true}
followUserLocation={true}>
{this.state.markers.map(marker => (
<MapView.Marker
key={marker.key}
coordinate={marker.coordinate}
/>
))}
</MapView.Animated>
</View>
);
}
}
更新18-01-2018:
在将近一个月没有做这项工作之后,我发现这段代码让我感到悲伤
更新18-01-2018:
我想我找到了一段代码,这段代码把悲伤抛进了混音中。但它现在可以在重新编写代码后工作。非常混乱。它可能只是Lihini敏锐的眼睛。你还没有定义这个.state.marker在任何地方都能工作。尝试先设置状态
getEarthquakes(){
return fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson')
.then(response => response.json())
.then(responseData =>{
var markers = [];
for (var i = 0; i < responseData.features.length; i++) {
var coords = responseData.features[i].geometry.coordinates;
var marker = {
key: responseData.features[i].id,
coordinate: {
latitude: coords[1],
longitude: coords[0],
}
}
markers.push(marker);
}
this.setState({
markers: markers,
loaded: true,
marker: markers[markers.length-1] // set marker state
});
}
).done();
}
现在请注意在下面的代码段中存储的状态
this.setState({
region: {
latitude: posData.coords.latitude,
longitude: posData.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
accuracy: posData.coords.accuracy
}
});
您正在设置this.state.region
,而您应该基于上述代码设置this.state.marker
。因此,请尝试将上面的内容更改为
this.setState({
marker: {
latitude: posData.coords.latitude,
longitude: posData.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
accuracy: posData.coords.accuracy
}
});
我的错。我改了密码
this.setState({markers:markers,loaded:true,marker:markers[markers.length-1]//set marker state})代码>我尝试为lat和long推送一个新状态,但它所进入的区域仍然是我设置为默认的区域。它应该是这样工作的,还是我遗漏了什么?也要更改标记状态,因为它指向以前添加的最后一个标记。我尝试随地吐痰,但它不想在我的原始区域集之后更新。我已经用状态更新了原始代码
代码。当你有时间看看有没有什么不对劲的时候,你能快点看一下吗?谢谢很抱歉消失了。在继续使用代码之前,我必须做其他工作。我发现了“bug”,在重命名了所有仍然不起作用的东西之后,我删除了一段我没有添加到这里的代码,对此表示抱歉`我更新了帖子以显示代码的错误。我拿出的代码是我需要的代码,所以那太糟糕了。
this.setState({
region: {
latitude: posData.coords.latitude,
longitude: posData.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
accuracy: posData.coords.accuracy
}
});
this.setState({
marker: {
latitude: posData.coords.latitude,
longitude: posData.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
accuracy: posData.coords.accuracy
}
});