Android-从MapsActivity向片段地图添加标记

时间:2018-10-02 10:15:41

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

我遇到了标题中写的问题

这是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中的标记放到片段中,因为这是在地图上打印标记的唯一方法。应用程序具有操作栏标签,并且只能通过片段显示地图?

1 个答案:

答案 0 :(得分:0)

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);
    }
}

根据需要进行修改。

相关问题