我如何使onLocationResult函数起作用?

时间:2019-07-18 05:42:50

标签: android google-maps-api-3

我正在尝试显示我的当前位置并将该位置存储到Firebase,并在我的当前位置放置一个标记。

我在google中搜索,甚至堆栈溢出都找不到与我的问题有关的任何内容。 我也读过Google的地图sdk文档,但听不懂。

LocationCallback mLocationCallback = new LocationCallback() { //NEW onLocation changed
        @Override
        public void onLocationResult(LocationResult locationResult) {

            for (Location location : locationResult.getLocations()) {
                mLastLocation = location;

                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(11));//1-21



            }
        }

    };

我希望在我的应用中看到一个蓝点,但只会看到没有任何标记甚至没有我当前位置的地图

3 个答案:

答案 0 :(得分:0)

看看这个:

// Creating a marker
        MarkerOptions markerOptions = new MarkerOptions();

        // Setting the position for the marker
        markerOptions.position(latLng);

        // Setting the title for the marker.
        // This will be displayed on taping the marker
        markerOptions.title(latLng.latitude + " : " + latLng.longitude);

这应该放在您的

上方

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(11));//1-21

答案 1 :(得分:0)

public fun placeMarkerOnMap(location: LatLng, drawableResInt: Int? = null): Marker? {
    val markerOptions = MarkerOptions().position(location)
    val marker = googleMap?.addMarker(markerOptions)

    if (drawableResInt != null) {
        marker?.setIcon(BitmapDescriptorFactory.fromResource(drawableResInt))
    }

    return marker
}

您的代码缺少添加标记的部分。您可以使用/不使用自定义图像将标记添加到特定的LatLng。然后,仅移动摄像机即可。

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng))

答案 2 :(得分:0)

通过执行以下操作添加标记

double latitude = location.getLatitude();
double longitude = location.getLongitude());
LatLng latlng= new LatLng(latitude, longitude); ////your lat lng
mMap.addMarker(new MarkerOptions().position(latlng));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(posisiabsen, 65);
mMap.animateCamera(cameraUpdate);