【问题标题】:How to fit a zoom in the specific marker in react native and can select any other marker in the map?如何在本机反应中放大特定标记并可以选择地图中的任何其他标记?
【发布时间】:2019-10-03 14:53:53
【问题描述】:

在这种情况下,当应用程序初始化时,Pin-marker 设置在当前位置,但在同一张地图中我有其他标记,当我尝试选择其他标记时,它再次适合当前位置,如何解决这个问题?我需要选择任何标记......并且......当我找到某个地方(带有谷歌位置)时,它会设置一个新标记并设置一个新位置并将相机设置为当前搜索标记并放大这个选定的标记,我的另一个问题是通过按钮获取当前位置(设备用户)的功能......有人可以帮助我吗???

<View style={styles.container}>
    <StatusBar hidden />
    <MapView
      onPress={this.handleMapPress}
      style={StyleSheet.absoluteFill}
      ref={map => (this.mapView = map)}
      rotateEnabled={true}
      scrollEnabled={true}
      showsMyLocationButton={true}
      followsUserLocation={true}
      showsUserLocation={true}
      zoomEnabled={true}
      showsPointsOfInterest={true}
      showBuildings={false}
      //region={this.props.region}
      initialRegion={region}
      provider="google">
      {!!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.Marker coordinate={this.props.region} >
          <Image
            source={ markerImage}
            style={styles.icon}
          />
      </MapView.Marker>
    </MapView>

这里是调用当前位置的按钮:

        <TouchableOpacity
       onPress={this.getlocation}
      style={styles.fab2}>
            <Image
              source={require('../assets/gps.png')}
              style={{
                width: 35,
                height: 35,
                margin: 10,
                tintColor: 'white',
              }}
            />
    </TouchableOpacity>

【问题讨论】:

  • Hey i tried to reproduce this but can't, with the code provided in your previous thread the map is already centered and zoomed into the marker, and this occurs whenever a new place is selected so i don '不明白这个问题,您能否编辑您的问题以澄清目前究竟是什么不起作用?
  • 嗨,Evan,我已经在 map-container.js 上粘贴了一个初始区域,以便直接在地图中打开。所以,现在我改变它,我把空白区域...地图,它再次更改为选定的 reg
  • 如果我将 更改为 它允许我在地图上的其他区域中选择其他标记,但是当我搜索其他区域时,它不适合该区域的相机,只是标记
  • 并将 region={this.props.region} 更改为 region={region}...
  • 我试着制作一个按钮来移动到当前位置,但我做不到,我也需要帮助

标签: google-maps react-native google-maps-markers


【解决方案1】:

我找到了解决这个问题的方法,这里是您阅读的零食......

下面是简单的代码:

  pickLocationHandler = event => {
const coords = event.nativeEvent.coordinate;

this.map.animateToRegion({
  ...this.state.focusedlocation,
  latitude: coords.latitude,
  longitude: coords.longitude,
});

this.setState(prevState => {
  return {
    focusedlocation: {
      ...prevState.focusedlocation,

      latitude: coords.latitude,

      longitude: coords.longitude,
    },

    locationChosen: true,
  };
});
};

getLocationHandler = () => {
navigator.geolocation.getCurrentPosition(
  pos => {
    const coordsEvent = {
      nativeEvent: {
        coordinate: {
          latitude: pos.coords.latitude,

          longitude: pos.coords.longitude,
        },
      },
    };

    this.pickLocationHandler(coordsEvent);
  },

  err => {
    console.log(err);

    alert(
      'Ocorreu um erro ao carregar sua localização, por favor ligue o GPS!'
    );
  }
 );
};

我在我创建的按钮上调用这个函数:

        <TouchableOpacity
     onPress={this.getLocationHandler}
      style={styles.fab2}>
            <Image
              source={require('../assets/gps.png')}
              style={{
                width: 35,
                height: 35,
                margin: 10,
                tintColor: '#FF3D00',
              }}
            />
    </TouchableOpacity>

这里是完整代码的链接:

https://snack.expo.io/@matheus_cbrl/react-native-maps-autocomplete---address---current-location-button

如果这回答了您可能遇到的问题,请喜欢它! :)

【讨论】:

  • 不错!所以你使用了focusedlocation变量。我了解到您使用的是this repo 并关注了this tutorial
  • 我实际上使用了你对我所做的代码,我让它为我的必需品工作。但是,地图上的命令this.props.region 功能不允许我选择其他引脚,如何在搜索时设置焦点?你能帮帮我吗?
  • 如果你打开完整的代码你会看到我评论了这个函数...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
  • 2020-06-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多