【问题标题】:How can I get this code to execute automatically in onMapReady如何让此代码在 onMapReady 中自动执行
【发布时间】:2019-10-21 22:13:03
【问题描述】:

我想从解析的 JSON GET 中比较纬度和经度。为了比较 GET 中的 lat/lng,我需要在 GET 发生之前与设备当前位置 lat/lng 进行比较。

我有一个使用 onMyLocationButtonClick 的方法,效果很好。但是,我需要的是执行 onMapReady 的相同方法,以便我可以将设备当前的 lat/lng 与从 JSON GET 响应中解析的内容进行比较。

即使在已擦除的模拟器上点击也能很好地工作,但问题是在调用 API 之前获取和分配设备当前的纬度/经度值。使用 onClick 方法,这些值仅在 onClick 获得,并且发生在 API 调用和 JSON 解析之后。

我尝试简单地调用onMyLocationClick(Location location),但我收到一个错误“无法解析位置符号”。我想我必须为 location 分配一个 locationListener ,但我不太确定该怎么做。过了一会儿,所有的阅读和研究都变成了汤,所以现在我开始寻求帮助。 :)

最终结果将是 onMapReady:

  1. 根据需要验证和请求位置权限。
  2. 在验证权限后,缩放到地图上的当前设备位置并分配当前纬度/经度值经纬度/经度变量。
  3. 通过 API 调用传递 lat/lng 变量值,以将当前 lat/lng 与从 API 检索到的 lat/lng 值进行比较。
    • 我相信这可以使用 getRetrofit(lat, lng) 来完成,就像调用 setAddress 一样。

非常感谢任何帮助。

onMapReady 方法:

    public void onMapReady(GoogleMap map) {
        mMap = map;

        mMap.setOnMyLocationButtonClickListener(this);
        mMap.setOnMyLocationClickListener(this);
        enableMyLocation();  // checks for fine and coarse location permissions
        // onMyLocationClick(Location location);
        getRetrofit(); //call to API
    }

The method I want to execute onMapReady before the call to the API without having to click the location button:

@Override
    public void onMyLocationClick(Location location) {

        // get device current lat/lng
        this.lat = location.getLatitude();
        this.lng = location.getLongitude();

        setAddress(lat, lng); // method to reverse geocode to physical address
        // zoom to current location on map
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));


        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                .zoom(17)                   // Sets the zoom
                .bearing(90)                // Sets the orientation of the camera to east
                .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

【问题讨论】:

    标签: android location latitude-longitude


    【解决方案1】:

    在您的 onCreate() 或您需要它运行的地方然后将逻辑从您的 onMyLocationClick 移动到一个新函数并在调用 onMapReady 之前调用它 onMapReady 就像在 onCreate() 和你的 onMyLocationClick

    
    
    GoogleMap map = getMyLocation(location)
    onMapReady(map)
    
    public map getMyLocation(Location location){
       this.lat = location.getLatitude();
            this.lng = location.getLongitude();
    
            setAddress(lat, lng); // method to reverse geocode to physical address
            // zoom to current location on map
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
    
    
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                    .zoom(17)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
    
    

    【讨论】:

    • 感谢您的回复 Chooven!如何在GoogleMap map = getMyLocation(location) 中获得原始位置的分配?编译器告诉我它无法解析符号location。
    • 啊,要获取您当前的位置,请查看此线程,它可以帮助我link
    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多