【问题标题】:when user turn On location show marker on current location当用户打开位置时在当前位置显示标记
【发布时间】:2017-12-05 12:02:53
【问题描述】:

我在其中创建了一张地图,我在当前位置显示了一个标记,它工作正常 当位置已经打开但是当活动打开并且我打开位置时它不显示任何标记我想在位置打开时显示标记 有什么帮助吗?..

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
         getCurrentLocation();
       // latLngCurrentLocation is my current location which is update using getCurrentLocation() method.

    if(latLngCurrentLocation != null) {

    // add marker on current location
            markerCurrent = mMap.addMarker(new MarkerOptions()
                    .position(latLngCurrentLocation)
                    .draggable(true)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                    .title("Current Location"));

            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLngCurrentLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(8));
            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.setOnMarkerDragListener(this);


        }

    }

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    通过 GpsTracker 类或 googleClient api,您可以获取纬度和经度,然后将其作为 gpstracker.getlatitude 和 getlongtitude 获取,并将此值设置为全局变量并在您想要的任何地方访问它。

    【讨论】:

    • 位置开启/关闭时有触发吗?
    • 无法回答您的问题。
    • 如果用户打开他的位置任何默认方法或任何其他触发我们可以识别用户打开/关闭位置的东西
    • 你需要添加一个对话框,如果gps如果关闭,则要求用户每次打开gps。请参考链接Link
    • 我已经准备好在位置上完成这件事,但是打开位置后我回到地图应用程序那里没有添加标记,我需要关闭应用程序并重新启动它以查看标记,但我没有“不想那样做@Shivam
    【解决方案2】:

    更改位置后,您将在onLocationChanged(Location location) 中获得位置。您将在 location 对象中获得纬度和经度,因此您可以将标记设置为 onLocationChanged(Location location) 方法

    @Override
    public void onLocationChanged(Location location)
    {
        mLastLocation = location;
        if (mCurrLocationMarker != null)
        {
            mCurrLocationMarker.remove();
        }
    
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
        mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
    
        //move map camera
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
    
    
    }
    

    或者,如果您想在 GPS 开启后刷新标记,则可以使用 BroadcastReceiver。当 GPS 关闭/关闭时会触发 BroadcastReciever,因此您可以检查 GPS 是否打开或关闭,您可以在以下链接中找到示例,Broadcast receiver for GPS

    【讨论】:

    • 感谢您的快速回复,但我真的希望不使用 OnLocation 更改方法
    • 发布您的getCurrentMethod() 代码@nileshprajapati
    • 它获取当前位置的方法,我只使用了 Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    • 您可以为此使用计时器。您需要从特定的时间间隔获取功能。 @nileshprajapati
    • 使用计时器我可以使用更多时间,但这对应用程序不利,它也会消耗更多内存和电池。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多