moveCamera() 在 Android 上使用了错误的位置数据

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

moveCamera() uses wrong location data on Android

问题

我希望你能帮助我。我的当前应用程序能够获取我的当前位置(通过GPS)。现在我在我的当前位置添加了一个标记,它工作得很好,但是如果我想将相机移动到标记,它就无法工作。moveCamera(position); 的输入与 addMarker(position); 的输入相同。

以下是我的代码:

// 当没有位置信息时使用center(柏林)
final CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(52.5075389, 13.5231758));
final CameraUpdate zoom = CameraUpdateFactory.zoomTo(14);

// 从其他活动获取位置信息
final Bundle extras = getIntent().getExtras();

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng position = new LatLng(52.5075389, 13.5231758);

        if (extras != null) { // 如果有位置信息
            position = (LatLng) extras.getParcelable("location");
            final CameraUpdate centerLocation = CameraUpdateFactory.newLatLng(position);

            MarkerOptions markerOptions = new MarkerOptions().position(position).title(MARKER_TITLE).snippet(MARKER_SNIPPET);
            googleMap.addMarker(markerOptions);

            googleMap.getUiSettings().setMapToolbarEnabled(false); // 隐藏自动生成的按钮
            googleMap.moveCamera(centerLocation);
            googleMap.animateCamera(zoom);
        } else {
            // 这里是其他一些代码
        }
    }
});

我的标记位于正确的位置,但是我的 centerLocation 不在正确的位置。有什么建议吗?

英文:

I hope you can help me. My current app is able to get my current location (via GPS). Now I add a marker to my current location which works perfect, but if I want to move my camera to the marker it doesn't work. The inputs for the moveCamera(positon); are the same as for addMarker(position);

here is my code:

//center is used when no location is available (Berlin)
final CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(52.5075389,13.5231758)); 
final CameraUpdate zoom = CameraUpdateFactory.zoomTo(14);

//get location from other activity
final Bundle extras = getIntent().getExtras();

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                LatLng position = new LatLng(52.5075389,13.5231758);

                if (extras != null) { //if location available
                    position = (LatLng)extras.getParcelable("location");
                    final CameraUpdate centerLocation = CameraUpdateFactory.newLatLng(position);

                    MarkerOptions markerOptions = new MarkerOptions().position(position).title(MARKER_TITLE).snippet(MARKER_SNIPPET);
                    googleMap.addMarker(markerOptions);

                    googleMap.getUiSettings().setMapToolbarEnabled(false); //hide auto created buttons
                    googleMap.moveCamera(centerLocation);
                    googleMap.animateCamera(zoom);
                }
    else{
//some other code here
}

My marker is at the correct position, but my centerLocation don't. Any suggestions?

答案1

得分: 0

多亏了评论。这是我的解决方案:

final float zoom = 14;

// 代码的其余部分保持不变

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, zoom));
英文:

thanks to the comments. here is my solution:

final float zoom = 14;

//rest of the code is unchaged

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, zoom));

huangapple
  • 本文由 发表于 2020年5月4日 21:46:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61593768.html
匿名

发表评论

匿名网友

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

确定