简体   繁体   English

Android Google Maps API v2中心缩放

[英]Android Google Maps API v2 Center Zoom

I have Google Maps set up with my application and can view the map with a blue dot indicating my location on the map, however everything I have tried to centre and zoom in on that location automatically does not seem to work. 我已经在应用程序中设置了Google Maps,并且可以通过蓝点查看地图,该蓝点指示我在地图上的位置,但是我尝试自动居中并自动放大该位置的所有操作似乎均不起作用。

When I press the target icon in the top right the camera view zooms in to my location which is what I want it to do automatically. 当我按右上角的目标图标时,摄像机视图将放大到我的位置,这是我希望它自动执行的操作。

I have tried the following: 我尝试了以下方法:

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

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

And also tried 并且还尝试了

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

Yet neither seem to be working. 但是似乎都没有用。 I have these lines of code in onLocationChanged as shown in a tutorial I have followed. 如我所遵循的教程中所示,我在onLocationChanged中有这些代码行。 I have attempted different suggestions from similar questions on here also and it still seems to be zoomed out, yet highlighting my current location in the correct position by a blue dot on the map. 我在这里也尝试过类似问题的不同建议,但它似乎仍然可以缩小,但在地图上的蓝点突出了我当前的位置。

Have I missed something obvious? 我错过了明显的事情吗? Thanks 谢谢

Edit: Entire activity:- 编辑:整个活动:-

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

}

}

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

myMap.getUiSettings().setZoomControlsEnabled(true);

You have followed a wrong tutorial. 您遵循了错误的教程。 If you want to get updates on blue dot location changes, use OnMyLocationChangeListener with your GoogleMap object instead of LocationManager . 如果要获取蓝点位置更改的更新, OnMyLocationChangeListener对您的GoogleMap对象(而不是LocationManager使用OnMyLocationChangeListener

These methods can return different results because GoogleMap uses fused location provider and LocationManager only raw GPS or network provider. 这些方法可以返回不同的结果,因为GoogleMap使用融合的位置提供程序,而LocationManager仅使用原始GPS或网络提供程序。

If you are worried about OnMyLocationChangeListener being deprecated, see my monologue here . 如果您担心OnMyLocationChangeListener被弃用,请在此处查看我的独白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM