【问题标题】:Android Maps - animateCamera() method not working properAndroid 地图 - animateCamera() 方法无法正常工作
【发布时间】:2013-05-28 16:05:37
【问题描述】:

问题:

1) 地图被动画化以到达所需位置(代码中的第 4 行),但它被缩放到默认位置(代码中的第 5 行)

[以指定的缩放级别将地图留在默认位置]

2) 我明白为什么会出现问题,但我不知道如何解决。

3) 如果我将第 4 行更改为 moveCamera 而不是 animateCamera 会起作用,但我确实想要 animateCamera() 方法。

代码如下:

map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
MarkerOptions options=new MarkerOptions().position(new LatLng(13.0810,80.2740));
map.addMarker(options);
map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(13.0810,80.2740)),4000,null);
map.animateCamera(CameraUpdateFactory.zoomTo(15.5f),2000,null);

【问题讨论】:

    标签: android google-maps-android-api-2 android-maps-v2


    【解决方案1】:

    问题是您在开始对新位置进行动画处理后立即调用zoom。这就是为什么它只是用新的替换最后一个相机更新操作。

    您可以通过创建更准确的相机更新操作(包括 latlng 更改和缩放级别更改)来简单地解决此问题:

    CameraPosition newCamPos = new CameraPosition(new LatLng(13.0810,80.2740), 
                                                      15.5f, 
                                                      map.getCameraPosition().tilt, //use old tilt 
                                                      map.getCameraPosition().bearing); //use old bearing
    map.animateCamera(CameraUpdateFactory.newCameraPosition(newCamPos), 4000, null);
    

    替代正如 MaciejGórski 指出的那样,您可以只使用 newLatLngZoom 接口,其中包括 LatLngzoom 更改:

    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.0810,80.2740), 15.5f), 4000, null);
    

    【讨论】:

    • 在工厂使用 newLatLngZoom 版本更容易。
    • 是的,你是对的 :) 我也会用这个选项更新我的答案
    • 请注意,动画在 lite 模式下不起作用,虽然一个简单的淡入动画会很好
    • @LouisCAD 精简模式是什么意思?免费版?
    • @ralphgabb 搜索 Google Maps Lite 模式是什么。例如,它用于静态地图或在列表中使用
    【解决方案2】:

    CancelableCallback 与第一个animateCamera 一起使用,并在onFinish 中调用第二个animateCamera

    示例:AnimateCameraChainingExampleActivity.java

    【讨论】:

      【解决方案3】:
        useEffect(() => {
          const fetchLocation = async () => {
            const hasLocationPermission =
              Platform.OS === 'ios'
                ? await Geolocation.requestAuthorization('whenInUse')
                : await PermissionsAndroid.request(
                    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
                  );
      
            if (hasLocationPermission === 'granted') {
              await Geolocation.getCurrentPosition(
                position => {
                  const {
                    coords: {latitude, longitude},
                  } = position;
      
                  setLocation({
                    latitude,
                    longitude,
                    latitudeDelta: 1,
                    longitudeDelta: 1,
                  });
                },
                error => {
                  // See error code charts below.
                  console.log(error.code, error.message);
                },
                {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
              );
            }
          };
      
          fetchLocation();
        }, []);
      
        useEffect(() => {
          if (location && _map.current) {
            _map.current.animateCamera(
              {
                center: {
                  latitude: location.latitude,
                  longitude: location.longitude,
                },
                zoom: 15,
              },
              {duration: 5000},
            );
          }
        }, [location]);
      
        return (
          <View style={styles.container}>
            <MapView style={styles.map} provider={PROVIDER_GOOGLE} ref={_map}>
              {location && <Marker coordinate={location} />}
            </MapView>
          </View>
        );
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-02
        • 2011-03-25
        相关资源
        最近更新 更多