英文:
I am not able to place marker according to search
问题
我正在尝试在我的项目中在搜索区域上放置标记。当前,当通过API获取数据时,默认情况下会将标记放置在固定坐标上。API中的坐标列表也被正确获取,但当我根据区域搜索列表时,数据被正确过滤,但标记不会移动。是否有解决这个问题的方法?
import { Carousal, Heading, Input, List, Map, Image } from '../components';
import axios from 'axios';
export const NearMe = () => {
const [responseData, setResponseData] = useState(undefined);
const [searchData, setSearchData] = useState([]);
const [search, setSearch] = useState('');
const [markerCoordinate, setMarkerCoordinate] = useState([]);
const getAPIData = async () => {
try {
const url = "https://903f-202-47-60-211.ngrok-free.app/listing";
let result = await fetch(url);
result = await result.json();
return result;
} catch (error) {
console.log('error')
console.error(error)
}
}
useState(async () => {
// const data = await getAPIData();
// setData(data);
try {
const url = "https://97cf-119-157-85-150.ngrok-free.app/listing";
let result = await fetch(url);
result = await result.json();
setResponseData(result.data);
setSearchData(result.data);
} catch (error) {
console.log('error')
console.error(error)
}
})
console.log(responseData)
const onSearch = (text) => {
setSearch(text);
const filteredData = responseData.filter((item) =>
item.listing_area.toUpperCase().includes(text.toUpperCase())
);
setSearchData(filteredData);
console.log(filteredData);
if (filteredData.length > 0) {
setMarkerCoordinate({
latitude: parseFloat(filteredData[0].listing_latitude),
longitude: parseFloat(filteredData[0].listing_longitude),
});
}
console.log(filteredData[0].listing_latitude);
};
const [carousalView, setCarousalView] = useState(true);
const navigation = useNavigation();
const toogleView = () => {
setCarousalView(!carousalView);
};
const navigationHandler = (data) => {
navigation.navigate(routes.VENUE_PROFILE, data);
};
//console.log(responseData);
return (
<SafeAreaView edges={['top', 'left', 'right']} style={styles.container}>
<View style={styles.mapContainer}>
<Map width="100%" height="100%" markerCoordinate={markerCoordinate} />
<Input placeholder="Search area"
placeholderTextColor="#aaaaaa"
style={styles.input}
value={search}
onChangeText={text =>
onSearch(text)
} />
</View>
<View style={styles.headingContainer}>
<Heading style={styles.heading}>Things To Do</Heading>
<TouchableOpacity onPress={toogleView}>
<Heading style={{ ...styles.heading, color: theme.colors.blue }}>
{carousalView ? 'List View' : 'Carousel View'}
</Heading>
</TouchableOpacity>
</View>
{carousalView ? (
<Carousal data={searchData} onPress={navigationHandler} />
) : (
<List data={searchData} onPress={navigationHandler} />
)}
</SafeAreaView>
);
};
地图组件
import React from 'react';
import { StyleSheet } from 'react-native';
import MapView, { Marker, PROVIDER_GOOGLE } from 'react-native-maps';
import { baseStyle } from '../config';
export const Map = ({ width, height, style }) => {
return (
<MapView
provider={PROVIDER_GOOGLE}
style={[styles.map, { width, height, ...style }]}
region={{
latitude: 24.83959,
longitude: 67.08204,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
}}
zoomEnabled
loadingEnabled
>
<Marker coordinate={{
latitude: 24.83959,
longitude: 67.08204
}}/>
</MapView>
);
};
const styles = StyleSheet.create({
map: {
borderRadius: baseStyle.borderRadius(24),
},
});
英文:
I am trying to place marker on searched area in my project. Currently it has been placed to a fixed coordinate by default when data is fetched through API. Listing co-ordinates in API are also fetched properly but when I searched for a listing on the basis of area data is filtered properly but marker does not move, any solution to the problem?
import { Carousal, Heading, Input, List, Map, Image } from '../components';
import axios from 'axios';
export const NearMe = () => {
const [responseData, setResponseData] = useState(undefined);
const [searchData, setSearchData] = useState([]);
const [search, setSearch] = useState('');
const [markerCoordinate, setMarkerCoordinate] = useState([]);
const getAPIData = async () => {
try {
const url = "https://903f-202-47-60-211.ngrok-free.app/listing";
let result = await fetch(url);
result = await result.json();
return result;
} catch (error) {
console.log(`error`)
console.error(error)
}
}
useState(async () => {
// const data = await getAPIData();
// setData(data);
try {
const url = "https://97cf-119-157-85-150.ngrok-free.app/listing";
let result = await fetch(url);
result = await result.json();
setResponseData(result.data);
setSearchData(result.data);
} catch (error) {
console.log(`error`)
console.error(error)
}
})
console.log(responseData)
const onSearch = (text) => {
setSearch(text);
const filteredData = responseData.filter((item) =>
item.listing_area.toUpperCase().includes(text.toUpperCase())
);
setSearchData(filteredData);
console.log(filteredData);
if (filteredData.length > 0) {
setMarkerCoordinate({
latitude: parseFloat(filteredData[0].listing_latitude),
longitude: parseFloat(filteredData[0].listing_longitude),
});
}
console.log(filteredData[0].listing_latitude);
};
const [carousalView, setCarousalView] = useState(true);
const navigation = useNavigation();
const toogleView = () => {
setCarousalView(!carousalView);
};
const navigationHandler = (data) => {
navigation.navigate(routes.VENUE_PROFILE, data);
};
//console.log(responseData);
return (
<SafeAreaView edges={['top', 'left', 'right']} style={styles.container}>
<View style={styles.mapContainer}>
<Map width="100%" height="100%" markerCoordinate={markerCoordinate} />
<Input placeholder="Search area"
placeholderTextColor="#aaaaaa"
style={styles.input}
value={search}
onChangeText={text=>
onSearch(text)
} />
</View>
<View style={styles.headingContainer}>
<Heading style={styles.heading}>Things To Do</Heading>
<TouchableOpacity onPress={toogleView}>
<Heading style={{ ...styles.heading, color: theme.colors.blue }}>
{carousalView ? 'List View' : 'Carousel View'}
</Heading>
</TouchableOpacity>
</View>
{carousalView ? (
<Carousal data={searchData} onPress={navigationHandler} />
) : (
<List data={searchData} onPress={navigationHandler} />
)}
</SafeAreaView>
);
};
Map Component
import React from 'react';
import {StyleSheet} from 'react-native';
import MapView, {Marker, PROVIDER_GOOGLE} from 'react-native-maps';
import {baseStyle} from '../config';
export const Map = ({width, height, style}) => {
return (
<MapView
provider={PROVIDER_GOOGLE}
style={[styles.map, {width, height, ...style}]}
region={{
latitude: 24.83959,
longitude: 67.08204,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
}}
zoomEnabled
loadingEnabled
>
<Marker coordinate={{
latitude: 24.83959,
longitude: 67.08204
}}/>
</MapView>
);
};
const styles = StyleSheet.create({
map: {
borderRadius: baseStyle.borderRadius(24),
},
});
答案1
得分: 1
在你的地图组件中,你已经设置了区域的静态值,将其更改为:
region={{
latitude: markerCoordinate.latitude,
longitude: markerCoordinate.longitude,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
}}
而在标记组件中,你也设置了默认值。这就是为什么标记位于静态位置的原因:
<Marker coordinate={markerCoordinate} />
不要忘记在地图组件中传递prop 'markerCoordinate':
export const Map = ({ width, height, style, markerCoordinate })
尝试一下,然后回复给我。
英文:
In your Map component you have set static values for the region change that into ,
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
region={{
latitude: markerCoordinate.latitude,
longitude: markerCoordinate.longitude,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
}}
<!-- end snippet -->
And in the Marker Component also you have set default values . That's why the marker is in static place,
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<Marker coordinate={markerCoordinate} />
<!-- end snippet -->
Don't forget to pass the prop 'markerCoordinate' in the Map Component,
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
export const Map = ({ width, height, style, markerCoordinate })
<!-- end snippet -->
Try it and revert back to me .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论