Skip to content

Instantly share code, notes, and snippets.

@16pxdesign
Created April 7, 2020 05:37
Show Gist options
  • Save 16pxdesign/545ca019d47f98e2673da6ceae649338 to your computer and use it in GitHub Desktop.
Save 16pxdesign/545ca019d47f98e2673da6ceae649338 to your computer and use it in GitHub Desktop.
package com.ruszala.fueltrack.ui.orders.map;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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 OrderMapPreviewFragment extends Fragment implements OnMapReadyCallback {
private OrderMapPreviewViewModel mViewModel;
private SupportMapFragment fragment;
private GoogleMap map;
public static OrderMapPreviewFragment newInstance() {
return new OrderMapPreviewFragment();
}
View root;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.order_map_preview_fragment, container, false);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(OrderMapPreviewViewModel.class);
// TODO: Use the ViewModel
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
fragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
Log.d("mylog", "OrderMapPreviewFragment 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;
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));
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.orders.map.OrderMapPreviewFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment