让Google地图自动放大我的当前位置?

 爵士723 发布于 2023-02-13 15:34

可能与Google Maps v2重复- 设置我的位置并放大

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_g_maps);
        GoogleMap googleMap;
        LatLng myPosition;


        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);

                if(location!=null){
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        myPosition = new LatLng(latitude, longitude);


    }

}
}

我试过添加:

CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(latitude,
                                                 longitude));
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

    googleMap.moveCamera(center);
    googleMap.animateCamera(zoom);

要么

 googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) );

但是,一旦我点击调用此方法的按钮,Google地图就不会自动放大.我仍然可以点击Google UI,我可以点击它来放大我的位置,但我希望它能自动放大.

有什么帮助吗?

1 个回答
  • 试试这个问题的简单解决方案

    LatLng coordinate = new LatLng(lat, lng);
    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
    map.animateCamera(yourLocation);
    

    并且..

    可以一次改变位置,变焦,方位和倾斜.也可以在animatecamera调用上设置持续时间.

    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
        .zoom(17)                   // Sets the zoom
        .bearing(90)                // Sets the orientation of the camera to east
        .tilt(30)                   // Sets the tilt of the camera to 30 degrees
        .build();                   // Creates a CameraPosition from the builder
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    

    看看这里的文档:

    https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera

    2023-02-13 15:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有