【问题标题】:Android - Add a Marker to a Fragment Map from MapsActivityAndroid - 从 MapsActivity 向片段地图添加标记
【发布时间】:2019-03-07 11:04:51
【问题描述】:

我有我在标题中写的问题

这是来自 MapsActivity 和片段的 2 个代码

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


            return;
        }
        map.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // redraw the marker when get location update.
                LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
                map.addMarker(new MarkerOptions().position(latlng).title("Marker").snippet(Float.toString(location.getSpeed())));
                map.moveCamera(CameraUpdateFactory.newLatLng(latlng));    
            }
       };
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 15, locationListener);
    }
}

片段映射

public class Tab2Map extends Fragment implements OnMapReadyCallback {
    GoogleMap map;
    public Tab2Map() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_tab2, container, false);

        return v;
    }
    @Override
    public  void onViewCreated(View view,@Nullable Bundle savedInstanceState){
        super.onViewCreated(view,savedInstanceState);
        SupportMapFragment mapFragment=(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.fragment);
        mapFragment.getMapAsync(this);

    }
    @Override
    public void onMapReady(GoogleMap googleMap) {

        }
}

我如何将 MapsActivity 中的标记放入片段中,因为这是在地图中打印的唯一方法。App 有操作栏选项卡,地图只能通过片段显示?

【问题讨论】:

  • 你有两张地图?
  • 不,我有一个,但我从 MapsActivity 中获取布局并将其放在片段中

标签: java android google-maps android-fragments google-maps-markers


【解决方案1】:

在onMapReady(GoogleMap googleMap)调用这个方法

 private void updateMap(List<SubOrdinateLocation> locations) {
    if (markers != null) {
        markers.clear();
        mGoogleMap.clear();
    }
    for (SubOrdinateLocation sol : locations) {
        if (sol.latitude != 0.0 && sol.longitude != 0.0) {
            LatLng sydney = new LatLng(sol.latitude, sol.longitude);
            Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(sydney).title(sol.subOrdinateId).snippet(sol.subOrdinateName));
            markers.add(marker);
        }
    }
    if (markers != null && markers.size() > 0) {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker marker : markers) {
            builder.include(marker.getPosition());
        }
        LatLngBounds bounds = builder.build();
        LatLng center = bounds.getCenter();
        builder.include(new LatLng(center.latitude - 0.001f, center.longitude - 0.001f));
        builder.include(new LatLng(center.latitude + 0.001f, center.longitude + 0.001f));
        bounds = builder.build();
        int padding = 0; // offset from edges of the map in pixels
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
        mGoogleMap.animateCamera(cu);
    }
}

根据您的需要修改它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多