【问题标题】:Moving marker to user location in Android studio在 Android Studio 中将标记移动到用户位置
【发布时间】:2019-04-17 22:18:27
【问题描述】:

我正在尝试制作一个地图活动应用程序,该应用程序在运行时会使用标记在地图上显示用户的 GPS 位置。但是,在运行时,它只会在默认位置(悉尼)显示标记,请帮助。下面是我的代码..我尝试了很多方法,但它们仍然把我带到了悉尼。我想要做的是让应用程序在运行后直接将我带到我的位置。 以下是我的代码;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_real_time_location);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);
}



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

    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            try {
                latLng = new LatLng(location.getLatitude(), location.getLongitude());

                mMap.addMarker(new MarkerOptions().position(latLng).title("My Current Position"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            }
            catch (SecurityException e){
                e.printStackTrace();
            }
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    try {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_UPDATE_TIME, MIN_UPDATES_DISTANCE, locationListener);
    }
    catch (SecurityException e){
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java android google-maps maps


    【解决方案1】:

    使用 LocationCallback 来获取您的最新位置:

    LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            List<Location> locationList = locationResult.getLocations();
            if (locationList.size() > 0) {
                //The last location in the list is the newest
                Location location = locationList.get(locationList.size() - 1);
                Log.i(TAG, "Location " + location.getLatitude() + " " + location.getLongitude());
    
                mLastLocation = location;
                if (mCurrLocationMarker != null) {
                    mCurrLocationMarker.remove();
                }
    
                //Create instance for current user lat and lng
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    
                // Add user position marker
                currentUserPositionMarker(latLng);
    
                //move map camera
                gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, CURRENT_MAP_ZOOM));
    
            }
        }
    };
    
    /**
     * Add Marker for user current position
     *
     * @param latLng current lat and lng of user
     */
    private void currentUserPositionMarker(LatLng latLng) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title(CURRENT_POSITION);
        mCurrLocationMarker = gMap.addMarker(markerOptions);
    
         //move map camera
         gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, CURRENT_MAP_ZOOM));
    }
    
    
    @Override
    public void onPause() {
        super.onPause();
    
        if (mFusedLocationProviderClient != null) {
            mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
        }
    }
    
    @SuppressLint("MissingPermission")
    @Override
    public void onMapReady(GoogleMap googleMap) {
        gMap = googleMap;
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.getUiSettings().setMapToolbarEnabled(false);
    
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(SET_INTERVAL_FOR_ACTIVE_LOCATION_UPDATES); // 3 seconds interval
        mLocationRequest.setFastestInterval(SET_INTERVAL_FOR_ACTIVE_LOCATION_UPDATES);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    
        if (isFineLocationPermissionGranted())
            mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
    }
    

    【讨论】:

      【解决方案2】:

      您需要获取设备的最后一个已知位置,使用this 链接获取最后一个已知位置,然后将标记显示在您想要的位置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        相关资源
        最近更新 更多