首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android地图-如何自动将地图居中到用户的当前位置?

Android地图-如何自动将地图居中到用户的当前位置?
EN

Stack Overflow用户
提问于 2018-11-01 21:49:49
回答 2查看 3.1K关注 0票数 1

我有一个Google活动,我希望一旦地图打开/蓝点出现,相机就会对用户进行对焦和放大,这样用户就不必按下“中心”按钮,也不必硬编码地图摄像机的位置。

我发现了很多5-6岁的代码示例,所以它们都不再起作用了。

是否可能获得当前用户的位置(lat )。而且很长。)更多?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-01 21:58:18

请试试这个:

代码语言:javascript
复制
private void centerOnMyLocation() {

    map.setMyLocationEnabled(true);

    location = map.getMyLocation();

    if (location != null) {
        myLocation = new LatLng(location.getLatitude(),
                location.getLongitude());
    }
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,
            Constants.MAP_ZOOM));
}

更新属性名称并调用此代码以当前用户的位置为中心。

如果您已经有了当前的职位,只需调用此功能:

代码语言:javascript
复制
map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,
                Constants.MAP_ZOOM));
票数 5
EN

Stack Overflow用户

发布于 2018-11-01 22:56:45

首先,您需要一个LatLngBounds对象,这是必要的,这样您就可以定义绑定映射的位置。

然后,您需要定义设备的宽度和高度,这非常简单。

只需遵循下面的代码

代码语言:javascript
复制
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//Insert your location
builder.include(location1);
builder.include(location2);
LatLngBounds bounds = builder.build();

//Get the width and height of the device's screen
int width = getResources().getDisplayMetrics().widthPixels;
int height = (int) ((getResources().getDisplayMetrics().heightPixels) * 0.85);
int padding = (int) (width * 0.15);

//Centralize the markers
yourMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53109835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档