android:无法在地图上添加当前位置

时间:2017-11-17 06:07:37

标签: android google-maps android-fragments android-maps-v2 android-gps

我在将当前/更新的位置放在具有其他数据的地图上时遇到问题。请看下面的图片。我需要它,因为我会在车辆位置和我当前的位置之间做一个距离,不管怎么做?此活动由片段扩展。我真的需要你的帮助。

enter image description here

这是我的OnMapReady

public void onMapReady(GoogleMap googleMap) {
    mMapProximity = googleMap;
    mMapProximity.setBuildingsEnabled(true);
    mMapProximity.getUiSettings().setRotateGesturesEnabled(false);

    setUpMap();
}
private void setUpMap() {
    mMapProximity.clear();
    mMapProximity.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMapProximity.setIndoorEnabled(true);
    mMapProximity.setBuildingsEnabled(true);
    mMapProximity.getUiSettings().setZoomControlsEnabled(false);

    final Double lati = Double.parseDouble(latitudeProxListMap);
    final Double longi = Double.parseDouble(longitudeProxListMap);

    m = mMapProximity.addMarker(new MarkerOptions()
            .position(new LatLng(lati, longi))
            .anchor(0.5f, 0.5f)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_logo))
            .visible(true));

    mMapProximity.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.showInfoWindow();
        }
    });

    mMapProximity.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lati, longi), 17.0f));

    mMapProximity.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(final Marker marker) {

            if (marker.isInfoWindowShown()) {
                marker.hideInfoWindow();
            } else {
                marker.showInfoWindow();
            }

            return true;
        }
    });

    mMapProximity.setInfoWindowAdapter(new MarkerInfoWindowAdapter());

    m.showInfoWindow();
}


class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {

        View v = getActivity().getLayoutInflater().inflate(R.layout.info_vehicle_list_map, null);
        TextView title =v.findViewById(R.id.title);
        TextView snippet =v.findViewById(R.id.snippet);
        snippet.setVisibility(View.GONE);
        title.setText("Location: " + location);
        return v;
    }

}

1 个答案:

答案 0 :(得分:1)

在您的活动中实现LocationListener,而不是在onLocationChange方法中使用当前位置值更新标记。这将在当前位置放置标记。