首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-native-maps fitToSuppliedMarker/fitToCorodinates不会显示所有标记

react-native-maps fitToSuppliedMarker/fitToCorodinates不会显示所有标记
EN

Stack Overflow用户
提问于 2018-10-17 22:03:47
回答 5查看 8.2K关注 0票数 3

我正在尝试使用简单的MapView和显示2个标记。

它们都正确地显示在地图视图上,但同时在屏幕上不可见(默认情况下渲染为Image1)

我必须执行手动拖动操作,才能在屏幕上看到这两个标记。(手动拖动地图后执行Image2,以便在屏幕上可以看到它们)

我尝试使用fitToSuppliedMarkers/ but,但似乎都不起作用。

代码语言:javascript
复制
export default class FavItemMapView extends Component{

constructor(props){
    super(props)
    this.state={
        position:{
            latitude:1.286353,
            longitude:103.853067,
            latitudeDelta: 0,
            longitudeDelta: 0,
        },
        error:null
    }
    this.mapRef = null;
}

componentWillMount(){

    const favPlaceLatitude = parseFloat(this.props.navigation.state.params.latitude)
    const favPlaceLongitude = parseFloat(this.props.navigation.state.params.longitude)
    markers.push({latitude:favPlaceLatitude,longitude:favPlaceLongitude});
    navigator.geolocation.getCurrentPosition(
        (position) => {
          console.log("wokeeey" + width + height);
          console.log(position);
          var lat = parseFloat(position.coords.latitude)
          var long = parseFloat(position.coords.longitude)

          var initialRegion = {
            latitude: lat,
            longitude: long,
            latitudeDelta: LATITUDE_DELTA,
            longitudeDelta: LONGITUDE_DELTA,
          }

          this.onRegionChange(initialRegion)
        },
        (error) => this.setState({ error: error.message }),
        { enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
      );
}

onRegionChange(region) {
    this.setState({ position:region })
}

componentDidMount(){
    //this.mapRef.fitToSuppliedMarkers(markerIDs,true);
    const favPlaceLatitude = parseFloat(this.props.navigation.state.params.latitude)
    const favPlaceLongitude = parseFloat(this.props.navigation.state.params.longitude)
    this.mapRef.fitToCoordinates([{latitude:favPlaceLatitude,longitude:favPlaceLongitude}], {
        edgePadding: {
          bottom: 200, right: 50, top: 150, left: 50,
        },
        animated: true,
    });
}

render(){

    const { navigation } = this.props;
    const favPlaceLatitude = parseFloat(navigation.state.params.latitude)
    const favPlaceLongitude = parseFloat(navigation.state.params.longitude)

    return(
        <View style={styles.container}>
            <MapView provider={MapView.PROVIDER_GOOGLE} ref={ref => {this.mapRef = ref;}} style={styles.map} region={this.state.position}>
                {this.state.position.latitude &&
                    <MapView.Marker identifier="Marker2" coordinate={{latitude:this.state.position.latitude,longitude:this.state.position.longitude}} pinColor="red"/>
                }
                {this.state.position.longitude &&
                    <MapView.Marker identifier="Marker1" coordinate={{latitude:favPlaceLatitude,longitude:favPlaceLongitude}} pinColor="green"/>
                }
            </MapView>
        </View>
    );
  }
}

const styles={
  container:{
    ...StyleSheet.absoluteFillObject,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  map:{
    padding:100,
    ...StyleSheet.absoluteFillObject
  }
}

你能告诉我哪里出了问题吗?附加模拟器图像以供参考。

EN

回答 5

Stack Overflow用户

发布于 2019-10-02 13:33:14

代码语言:javascript
复制
<MapView
    provider={MapView.PROVIDER_GOOGLE}
    ref={map => {this.map = map}}
    style={{ flex: 1 }}
    initialRegion={{latitude:this.state.request.latitude,
      longitude:this.state.request.longitude,
      latitudeDelta: 0.0922,
      longitudeDelta: 0.0421,}}
    onMapReady={() => {this.map.fitToSuppliedMarkers(['mk1','mk2'],{ edgePadding: 
      {top: 50,
        right: 50,
        bottom: 50,
        left: 50}

    })}}
    >
    <Marker
    image={require('../../../assets/pin.png')}
    coordinate={{latitude:this.state.request.latitude,longitude:this.state.request.longitude}} 
    title='Punto de partida'
    identifier={'mk1'}
    >

    </Marker>

    <Marker
    image={require('../../../assets/pin_final.png')}
    coordinate={{latitude:this.state.request.latitude_destinity,longitude:this.state.request.longitude_destinity}} 
    title='Punto de destino'
    identifier={'mk2'}
>
票数 10
EN

Stack Overflow用户

发布于 2019-06-25 04:18:36

因此,从我使用这个库的经验来看,我发现最好的体验是在MapView组件上使用onMapReady属性。

https://github.com/react-native-community/react-native-maps/blob/master/docs/mapview.md#events

代码语言:javascript
复制
<MapView
  ...
  onMapReady{() => {
    this.mapRef.fitToCoordinates(...)
  }}
/>

代码语言:javascript
复制
<MapView
  ...
  onMapReady{() => {
    this.mapRef.fitToSuppliedMarkers(...)
  }}
/>

让我知道这是否有帮助,我仍在学习它,但这种方法假设您已经准备好要传递到Map组件中渲染的标记。

票数 3
EN

Stack Overflow用户

发布于 2020-10-18 02:18:10

在我看来,最好的方法是在iOS中使用fitToElements,在安卓中使用fitToSuppliedMarkers (或fitToCoordinates)。我得到的代码最终是这样的:

代码语言:javascript
复制
import React, { useRef, useCallback } from 'react';
import { Platform } from 'react-native';
import MapView, { PROVIDER_GOOGLE, LatLng, Marker } from 'react-native-maps';

type Props = {
  origin: LatLng;
  destination: LatLng;
}

export default function ({ origin, destination }: Props) {
    const ref = useRef<MapView>(null);

    // effects
    const onMapReadyHandler = useCallback(() => {
      if (Platform.OS === 'ios') {
        ref?.current?.fitToElements(false);
      } else {
        ref?.current?.fitToCoordinates([origin, destination], {
          animated: true,
          edgePadding: {
            top: 150,
            right: 50,
            bottom: 50,
            left: 50,
          },
        });
      }
    }, [ref]);

    // UI
    return (
      <MapView
        style={{ width: '100%', height: '100%' }}
        ref={ref}
        provider={PROVIDER_GOOGLE}
        onMapReady={onMapReadyHandler}
      >
        <Marker coordinate={origin} identifier="origin" />
        <Marker coordinate={destination} identifier="destination" />
      </MapView>
    );
  }
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52856815

复制
相关文章

相似问题

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