Created
April 6, 2020 05:25
-
-
Save 16pxdesign/1855f1eebd81bcb3ecbdef780c00ccff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ruszala.fueltrack.ui.home; | |
import android.content.Context; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.fragment.app.Fragment; | |
import androidx.lifecycle.ViewModelProviders; | |
import com.google.android.gms.maps.CameraUpdate; | |
import com.google.android.gms.maps.CameraUpdateFactory; | |
import com.google.android.gms.maps.GoogleMap; | |
import com.google.android.gms.maps.MapView; | |
import com.google.android.gms.maps.OnMapReadyCallback; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.android.gms.maps.model.LatLngBounds; | |
import com.google.android.gms.maps.model.Marker; | |
import com.google.android.gms.maps.model.MarkerOptions; | |
import com.ruszala.fueltrack.R; | |
import com.ruszala.fueltrack.domain.Order; | |
import java.util.ArrayList; | |
public class HomeFragment extends Fragment implements OnMapReadyCallback { | |
private HomeViewModel homeViewModel; | |
private MapView map; | |
private String MAP_BUNDLE_KEY; | |
private LatLngBounds mBounds; | |
public View onCreateView(@NonNull LayoutInflater inflater, | |
ViewGroup container, Bundle savedInstanceState) { | |
homeViewModel = | |
ViewModelProviders.of(this).get(HomeViewModel.class); | |
View root = inflater.inflate(R.layout.fragment_home, container, false); | |
map = root.findViewById(R.id.map_order); | |
initGoogleMap(savedInstanceState); | |
return root; | |
} | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
private boolean isStatusOfGPS() { | |
LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); | |
return manager.isProviderEnabled(LocationManager.GPS_PROVIDER); | |
} | |
@Override | |
public void onMapReady(final GoogleMap googleMap) { | |
Log.d("mylog", "HomeFragment onMapReady: "); | |
ArrayList<Order> orders = new ArrayList<>(); | |
Order order = new Order(); | |
order.setPosition(new LatLng(56.461430, -2.968110)); | |
Order order2 = new Order(); | |
order2.setPosition(new LatLng(56.484008, -2.993350)); | |
Order order3 = new Order(); | |
order3.setPosition(new LatLng(56.396051, -3.054269)); | |
orders.add(order); | |
orders.add(order2); | |
orders.add(order3); | |
LatLngBounds.Builder builder = new LatLngBounds.Builder(); | |
MarkerOptions position = new MarkerOptions().position(order.getPosition()); | |
Marker marker = googleMap.addMarker(position); | |
marker.remove(); | |
googleMap.addMarker(new MarkerOptions().position(order2.getPosition())); | |
googleMap.addMarker(new MarkerOptions().position(order3.getPosition())); | |
for(Order o:orders){ | |
} | |
builder = new LatLngBounds.Builder(); | |
builder.include(order.getPosition()); | |
builder.include(order2.getPosition()); | |
builder.include(order3.getPosition()); | |
final LatLngBounds bounds = builder.build(); | |
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() { | |
@Override | |
public void onMapLoaded() { | |
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200)); | |
} | |
}); | |
} | |
private void initGoogleMap(Bundle savedInstanceState) { | |
Bundle mapViewBundle = null; | |
if (savedInstanceState != null) { | |
mapViewBundle = savedInstanceState.getBundle(MAP_BUNDLE_KEY); | |
} | |
map.onCreate(mapViewBundle); | |
map.getMapAsync(this); | |
} | |
@Override | |
public void onPause() { | |
map.onPause(); | |
super.onPause(); | |
} | |
@Override | |
public void onDestroy() { | |
map.onDestroy(); | |
super.onDestroy(); | |
} | |
@Override | |
public void onLowMemory() { | |
super.onLowMemory(); | |
map.onLowMemory(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
map.onResume(); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
map.onStart(); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
map.onStop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment