Google地图未通过位置更改更新LatLong

时间:2018-02-19 11:38:49

标签: android android-location

首先,我完全混淆了各种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);

        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

Move marker with gps in google map android