Android Google Maps API v2中心缩放

时间:2014-03-11 18:18:20

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

我已经使用我的应用程序设置了Google地图,并且可以使用指示我在地图上的位置的蓝点查看地图,但是我尝试居中并放大该位置的所有内容似乎都无效。

当我按下右上角的目标图标时,相机视图会放大到我想要它自动执行的位置。

我尝试了以下内容:

LatLng latlngPos = new LatLng(location.getLatitude(), location.getLongitude())

myMap.moveCamera(CameraUpdateFactory.newLatLng(latlngPos));
myMap.animateCamera(CameraUpdateFactory.zoomTo(15));

还试过

myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngPos, 15));

但似乎都没有奏效。我在onLocationChanged中有这些代码行,如我所遵循的教程中所示。我也在这里尝试了类似问题的不同建议,它似乎仍然缩小了,但是在地图上用蓝点突出显示我当前位置的正确位置。

我错过了一些明显的东西吗?谢谢

编辑:整个活动: -

public class FindBankActivity extends Activity implements LocationListener {

private GoogleMap myMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Make sure Google Play Services available
    if(isGooglePlay()){
        setContentView(R.layout.activity_find_bank);
        setUpMap();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.find_bank, menu);

    return true;
}

public boolean isGooglePlay() {

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if(status == ConnectionResult.SUCCESS) {
        return(true);
    }
    else
    {
        ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 15)).show();
        //Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
    }
    return(false);
}

public void setUpMap() {

    if(myMap == null) {

        myMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();


        if(myMap != null) {
            //Initiate map  

            myMap.setMyLocationEnabled(true);

            LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

            String provider = lm.getBestProvider(new Criteria(), true);

            if(provider == null) {
                onProviderDisabled(provider);
            }

            Location loc = lm.getLastKnownLocation(provider);

            if(loc == null) {

                LocationListener locListener = new LocationListener() {

                    @Override
                    public void onLocationChanged(Location location) {
                        // TODO Auto-generated method stub

                        double lat = location.getLatitude();
                        double lng = location.getLongitude();

                        LatLng ll = new LatLng(lat,lng);

                        myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 15));

                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onStatusChanged(String provider,
                            int status, Bundle extras) {
                        // TODO Auto-generated method stub

                    }
                };

                lm.requestLocationUpdates(provider, 20000, 0, locListener);

            }

                if(loc != null) {
                    onLocationChanged(loc);
                }
        }
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub

    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

}

@Override
public void onLocationChanged(Location location) {

    //Get the location
    LatLng latlngPos = new LatLng(location.getLatitude(), location.getLongitude());


    //Display on the map and zoom in 
//      myMap.moveCamera(CameraUpdateFactory.newLatLng(latlngPos));
//      myMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlngPos, 15));

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

2 个答案:

答案 0 :(得分:0)

我猜你忘了添加MAP-UI设置

myMap.getUiSettings().setZoomControlsEnabled(true);

答案 1 :(得分:0)

您遵循了错误的教程。如果您想获得有关蓝点位置更改的更新,请将OnMyLocationChangeListenerGoogleMap对象一起使用,而不是LocationManager

这些方法可能会返回不同的结果,因为GoogleMap仅使用融合位置提供商和LocationManager原始GPS或网络提供商。

如果您担心OnMyLocationChangeListener被弃用,请参阅my monologue here