【问题标题】:Google map is not updating LatLong with location change谷歌地图不会随着位置变化而更新 LatLong
【发布时间】:2018-07-29 15:25:33
【问题描述】:

首先,我对可用于位置和地图的各种 api 完全感到困惑。所以,我什至不知道代码是否最适合 sdk 27​​。(我设置了最低 sdk 24。)

问题是这段代码很好地显示了当前位置,但是当位置改变时地图会更新。但是,这并没有反映在位置上。看,从LatLong 绘制的折线不跟随当前位置。

另外,我正在尝试实现FusedLocationProviderClient,但这里可能不是这样。

非常欢迎任何帮助。

public class SecondFragment extends Fragment implements OnMapReadyCallback {

    //Color stroke = new Color(222.0,135.0, 135.0,174.0);

    private GoogleMap mMap;

    public static SecondFragment newInstance() {
        SecondFragment fragment = new SecondFragment();
        return fragment;
    }

    public GoogleMap getMap() {
        return mMap;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, null, false);

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        return view;
    }

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

        double latitude;
        double longitude;


        LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(getContext(),
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(getActivity(), new String[]
                    {android.Manifest.permission.ACCESS_FINE_LOCATION,
                            android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);

        } else {

            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location == null) {
                latitude = 0.0;
                longitude = 0.0;
            } else {
                latitude = location.getLatitude();
                longitude = location.getLongitude();

            }
            latlang.Lat = latitude;
            latlang.Lang = longitude;

            CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(latitude, longitude)).zoom(18).build();


            double angle = Math.toRadians(93.833);

            Point cPoint = mMap.getProjection().toScreenLocation(new LatLng(latitude,longitude));
            System.out.println("mPoint X"+  cPoint.x);
            System.out.println("mPoint Y"+  cPoint.y);
            Point mPoint = new Point();

            //Azimuth Pos only
            double lat_azi = 2*Math.sin(angle)+cPoint.y;
            double long_azi = 2*Math.cos(angle)+cPoint.x;

            LatLng nat_azi =mMap.getProjection().fromScreenLocation(mPoint);

            //private List<Point> mPoint = new ArrayList<Point>();
            Marker marker = mMap.addMarker(new MarkerOptions()
                    .position(nat_azi)
                    .anchor(0.5f,0.5f)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.sun_pos_icon))
                );
            Marker marker_loc = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude, longitude))
                    .anchor(0.5f, 0.5f)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
            );

            mMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(lat_azi, long_azi), new LatLng(latitude, longitude), new LatLng(0,0))
                    .width(15)
                    .color(Color.MAGENTA));

            PathOfSun.getTime();


            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            googleMap.getUiSettings().setZoomControlsEnabled(true); // true to enable
            googleMap.getUiSettings().setZoomGesturesEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setRotateGesturesEnabled(true);

        }
    }
}

【问题讨论】:

  • 您需要在“onLocationChanged”回调中进行处理,以便重绘线条/形状。请注意,您必须擦除以前的并在每次位置更改时绘制一个新的(假设这是您想要的),这意味着保持对折线的引用,例如请参阅 LocationServices.FusedLocationApi.requestLocationUpdates() - 如果使用融合。

标签: android android-location


【解决方案1】:

您可以按照以下链接更新地图上的标记位置。

Move marker with gps in google map android

【讨论】:

  • 感谢您的回复,但这是一个令人困惑的地方:FusedLocationProviderApi 已弃用。
  • 是的,现在已经被取消了。但根据具体要求,仅供参考。如果您想不断更新位置,则需要从 sn-p 中删除该代码。 FusedLocationProviderAPI 的另一种选择是 FusedLocationProviderClient。
  • 也供您参考。我非常卡在地图和位置上,实际上不知道如何在片段中启用它。
  • 我建议您使用 MapView 而不是 MapFragment。 developers.google.com/android/reference/com/google/android/gms/… inducesmile.com/android/android-mapview-example-tutorial 可能它会解决您在片段中的地图问题
猜你喜欢
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2017-08-30
  • 2013-02-12
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
相关资源
最近更新 更多