首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在GooglePlacesAutocomplete中设置本机响应标记

如何在GooglePlacesAutocomplete中设置本机响应标记
EN

Stack Overflow用户
提问于 2019-09-18 16:34:12
回答 1查看 1.6K关注 0票数 1

我需要帮助来解决这个问题。

问题在于我们在google位置选择器中搜索地址,然后它在选定的位置自动设置一个标记/引脚,但是标记集还不能工作。

屏幕是:下面的Diurno.js是整个代码。

以下是搜索代码:

代码语言:javascript
复制
      <GooglePlacesAutocomplete
    placeholder="Outro Local?"
    placeholderTextColor="#333"
    returnKeyType={'search'} // Can be left out for default return key 
    //onPress={onLocationSelected}
    onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
                this.props.notifyChange(details.geometry.location);
            }
            }
    query={{
      key: "",
      language: "pt"
    }}
        nearbyPlacesAPI='GooglePlacesSearch'
        debounce={300}
    textInputProps={{
      onFocus: () => {
        this.setState({ searchFocused: true });
      },
      onBlur: () => {
        this.setState({ searchFocused: false });
      },
      autoCapitalize: "none",
      autoCorrect: false
    }}
    listViewDisplayed={searchFocused}
    fetchDetails ={true}
    enablePoweredByContainer={false}

这里的其他代码:

代码语言:javascript
复制
        <MapView 
      //onPress={this.handleMapPress}
      onPress={this.handleMapPress}  
      style={StyleSheet.absoluteFill}
      ref={map => this.mapView = map}
      rotateEnabled={true}
      scrollEnabled={true}
      showsMyLocationButton={true}
      showsUserLocation={true}
      zoomEnabled={true}
      showsPointsOfInterest={true}
      showBuildings={false} 
      //initialRegion={initialRegion}
      //region={region}
      initialRegion={region}
      provider="google"> 
      {!!props &&
       (<MapView.Marker
                coordinate={props.region}
               >         
        </MapView.Marker>
        )} 

      {!!location &&
       (<MapView.Marker
                coordinate={location}
               onPress={this.handleMarkerPress} 
               >    
        <Image source={isAddressVisible? placholder2: placholder} style={styles.icon} />         
        </MapView.Marker>
        )} 
    {this.state.coordinates.map((coordinates, index, title, description, location) =>(
      <MapView.Marker 
      onPress={this.handleMapPress} 
      ref={mark => coordinates.mark = mark}
      key={`coordinate_${index}`} 
      title={coordinates.title}
      description={coordinates.description} 
      coordinate={{
            latitude: coordinates.latitude,
            longitude: coordinates.longitude,
            }}
      >
      <Image source={isAddressVisible? placholder2: placholder} style={styles.icon} /> 
    </MapView.Marker>
    ))}
  </MapView> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-21 08:50:01

我可以在你的代码中看到几个问题。首先,您将得到以下错误:

_this.notifyChange不是一个函数。

您需要在这里使用props

代码语言:javascript
复制
onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
    this.props.notifyChange(details.geometry.location);
  }
}

然后,请注意,您已经从您的region中注释掉了MapView属性,并且也没有onRegionChange。如果你加上这些:

代码语言:javascript
复制
region={this.props.region}
onRegionChange={(region) => this.props.onRegionChange(region)}

您的地图现在从自动完成预测中心到选定的地方。但是没有标记,因为您还应该相应地设置它的坐标:

代码语言:javascript
复制
<MapView.Marker coordinate={this.props.region} />

我运行了您的应用程序,在上面的更改之后,您的标记将被放置在选定的自动完成位置。见下面的截图。:)

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

https://stackoverflow.com/questions/57997244

复制
相关文章

相似问题

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