在Android V2中移动地图

时间:2014-09-14 02:31:15

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

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

如何更新地图以滚动当前位置?与谷歌地图应用程序一样。

@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) 
        {

        }
    });
    }

谢谢!

2 个答案:

答案 0 :(得分:2)

我认为您只需要删除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);

你有那个原因吗?

答案 1 :(得分:0)

如规范中所述:

  

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));