如何获得此代码以在onMapReady

时间:2019-10-21 22:13:03

标签: android location latitude-longitude

我想比较已解析的JSON GET中的纬度和经度。为了比较GET中的纬度/经度,我需要在GET发生前将设备的当前纬度/经度与设备进行比较。

我有一个使用onMyLocationButtonClick的方法,并且效果很好。但是,我需要使同一方法执行onMapReady,因此我可以将设备当前的经纬度与从JSON GET响应中解析的内容进行比较。

即使在已擦除的仿真器上单击也可以很好地工作,但是问题是在调用API之前获取并为设备分配了当前的lat / lng值。使用onClick方法,这些值仅是在onClick上获得的,并且发生在进行API调用并解析JSON之后。

我尝试简单地致电onMyLocationClick(Location location),但收到错误消息“无法解析位置符号”。我想我必须将locationListener分配给location,但是我不太确定该怎么做。一段时间后,所有的阅读和研究都变成了汤,所以现在我要寻求帮助。 :)

最终结果将是onMapReady:

  1. 根据需要验证并请求位置权限。
  2. 经许可验证后,缩放到地图上的当前设备位置并分配当前的lat / lng值lat / lng变量。
  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));
    }

1 个答案:

答案 0 :(得分:0)

在您的onCreate()中或您需要运行它的位置

,然后将逻辑从onMyLocationClick移至新函数,并在调用getMyLocation之前将其调用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));
}