【问题标题】:Move Map in Android V2在 Android V2 中移动地图
【发布时间】:2014-09-14 02:31:15
【问题描述】:

我已经为 Android 应用实现了 Google 地图 v2。我的位置,标记一切都显示正常,但是当我移动时 OnMylocationChange 被调用并且标记被更新,但是当我的位置视图离开地图时,我必须向上或向下滚动。

如何更新我的地图以滚动当前位置?喜欢 Google 地图应用。

@Override
public void onMyLocationChange(Location location) 
{

    if (markermylocation != null)
    {
        markermylocation.remove();
    }

    curlat = location.getLatitude();
    curlong = location.getLongitude();

    myLocation(curlat, curlong, username, imageURL, ZOOMVALUE);
}


private void myLocation(double lat, double lng, String name, String url, float zoom)
{
    if(firsttime  == 1)
    {
        LatLng ll = new LatLng(lat,lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        googleMap.animateCamera(update);
        firsttime = 0;
    }

    final String uname = name; 
    curlat = lat;
    curlong = lng;

    Picasso.with(getActivity()).load(url).resize(120, 120).transform(transformation).into(new Target() 
    {
        @Override
        public void onBitmapFailed(Drawable arg0) 
        {
            markerOptionsmylocaiton = new MarkerOptions().position(new LatLng(curlat, curlong)).icon(BitmapDescriptorFactory.fromResource(R.drawable.profilepic)).title(uname).anchor(0.5f, 1f);
            markermylocation = googleMap.addMarker(markerOptionsmylocaiton);          
        }

        @Override
        public void onBitmapLoaded(Bitmap b, LoadedFrom arg1) 
        {

            bitmapMarkermylocation = BitmapDescriptorFactory.fromBitmap(b);

            if(b != null)
            {
                markerOptionsmylocaiton = new MarkerOptions().position(new LatLng(curlat, curlong)).icon(bitmapMarkermylocation).title(uname).anchor(0.5f, 1f);
            }
            else
            {
                markerOptionsmylocaiton = new MarkerOptions().position(new LatLng(curlat, curlong)).icon(BitmapDescriptorFactory.fromResource(R.drawable.profilepic)).title(uname).anchor(0.5f, 1f);
            }

            markermylocation = googleMap.addMarker(markerOptionsmylocaiton);       
        }

        @Override
        public void onPrepareLoad(Drawable arg0) 
        {

        }
    });
    }

谢谢!

【问题讨论】:

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


    【解决方案1】:

    我认为您只需要删除 if (firsttime == 1) 条件,那么只要您的位置更新,地图就应该重新定位。

        LatLng ll = new LatLng(lat,lng);
        CameraUpdate update = null;
    
        if(firsttime  == 1) {
            update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
            firsttime = 0;
        } else {
            update = CameraUpdateFactory.newLatLng(ll);
        }
        googleMap.animateCamera(update);
    

    你有那个理由吗?

    【讨论】:

    • 问题是当我缩小以检查其他标记时。它会自动更改为我的位置,因此我看不到它们再次缩放。还有一个原因。我在某处读到 animateCamera 通常应该在第一次使用并且不需要 onMyLocation 更改。
    • 我更新了我的答案以反映您的评论,这应该只是第一次放大,所以如果你缩小它只会重新定位地图。最终你需要管理相机,让它做你想做的事。
    • @ nPN - 它正在处理移动到我当前位置的地图,但问题是在缩放到达我的第一次位置之前它会到达其他部分,因此第一次缩放没有根本不工作。 :(
    • 我对您的逻辑进行了一些更改,现在它正在运行。感谢您的帮助!
    • 很高兴听到,我打算建议您第一次使用 moveCamera,或者您可以考虑使用带有回调的 animateCamera 版本,该回调会在动画完成时运行。基本上你只需要玩,直到你得到你想要的视觉效果。
    【解决方案2】:

    如规范中所述:

    CameraUpdateFactory 是一个类,其中包含用于创建更改地图相机的 CameraUpdate 对象的方法。要修改地图的相机,请使用使用此类创建的 CameraUpdate 对象调用 animateCamera(CameraUpdate)、animateCamera(CameraUpdate, GoogleMap.CancelableCallback) 或 moveCamera(CameraUpdate)。 read more

    所以当你实现这个时:

    markermylocation = googleMap.addMarker(markerOptionsmylocaiton); 
    

    使用以下方法处理相机移动:

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    

    【讨论】:

    • @ Ali Abdolahi:但问题是我缩小地图时会立即定位到我的位置,因此我将无法再次缩小。如何使用您的解决方案解决该问题。
    • 尝试测试,可以通过调用GoogleMap.getUiSettings从GoogleMap获取UiSettings类,然后使用UiSettings.setZoomGesturesEnabled(true)来启用缩放手势
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多