Reputation: 11
I have the problem which I write in the title
Here are the 2 codes from MapsActivity and fragment
@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);
}
}
Fragment Map
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) {
}
}
Ηow can I put the marker from MapsActivity into the fragment because it's the only way to print it in map.App has Action Bar Tabs and the map can be displayed only through fragment?
Upvotes: 0
Views: 31
Reputation: 4569
Call this method in 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);
}
}
Modify it as per your needs.
Upvotes: 1