我无法根据搜索放置标记。

huangapple go评论65阅读模式
英文:

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 &#39;../components&#39;;
import axios from &#39;axios&#39;;
export const NearMe = () =&gt; {
const [responseData, setResponseData] = useState(undefined);
const [searchData, setSearchData] = useState([]);
const [search, setSearch] = useState(&#39;&#39;);
const [markerCoordinate, setMarkerCoordinate] = useState([]);
const getAPIData = async () =&gt; {
try {
const url = &quot;https://903f-202-47-60-211.ngrok-free.app/listing&quot;;
let result = await fetch(url);
result = await result.json();
return result;
} catch (error) {
console.log(`error`)
console.error(error)
}
}
useState(async () =&gt; {
// const data = await getAPIData();
// setData(data);
try {
const url = &quot;https://97cf-119-157-85-150.ngrok-free.app/listing&quot;;
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) =&gt; {
setSearch(text);
const filteredData = responseData.filter((item) =&gt;
item.listing_area.toUpperCase().includes(text.toUpperCase())
);
setSearchData(filteredData);
console.log(filteredData);
if (filteredData.length &gt; 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 = () =&gt; {
setCarousalView(!carousalView);
};
const navigationHandler = (data) =&gt; {
navigation.navigate(routes.VENUE_PROFILE, data);
};
//console.log(responseData);
return (
&lt;SafeAreaView edges={[&#39;top&#39;, &#39;left&#39;, &#39;right&#39;]} style={styles.container}&gt;
&lt;View style={styles.mapContainer}&gt;
&lt;Map width=&quot;100%&quot; height=&quot;100%&quot; markerCoordinate={markerCoordinate} /&gt;
&lt;Input placeholder=&quot;Search area&quot;
placeholderTextColor=&quot;#aaaaaa&quot;
style={styles.input}
value={search}
onChangeText={text=&gt;
onSearch(text)
} /&gt;
&lt;/View&gt;
&lt;View style={styles.headingContainer}&gt;
&lt;Heading style={styles.heading}&gt;Things To Do&lt;/Heading&gt;
&lt;TouchableOpacity onPress={toogleView}&gt;
&lt;Heading style={{ ...styles.heading, color: theme.colors.blue }}&gt;
{carousalView ? &#39;List View&#39; : &#39;Carousel View&#39;}
&lt;/Heading&gt;
&lt;/TouchableOpacity&gt;
&lt;/View&gt;
{carousalView ? (
&lt;Carousal data={searchData} onPress={navigationHandler} /&gt;
) : (
&lt;List data={searchData} onPress={navigationHandler} /&gt;
)}
&lt;/SafeAreaView&gt;
);
};

Map Component

import React from &#39;react&#39;;
import {StyleSheet} from &#39;react-native&#39;;
import MapView, {Marker, PROVIDER_GOOGLE} from &#39;react-native-maps&#39;;
import {baseStyle} from &#39;../config&#39;;
export const Map = ({width, height, style}) =&gt; {
return (
&lt;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
&gt;
&lt;Marker coordinate={{
latitude: 24.83959,
longitude: 67.08204
}}/&gt;
&lt;/MapView&gt;
);
};
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 -->

  &lt;Marker coordinate={markerCoordinate} /&gt;

<!-- 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 .

huangapple
  • 本文由 发表于 2023年6月29日 00:58:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定