Added MapMarker Class
Added Global accessible context Implementation of MapActivityViewModel Added observers to MapActivity Display all markers on the map thanks to the ViewModel
This commit is contained in:
parent
96080a182c
commit
6be2b7a371
@ -11,6 +11,7 @@
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
|
||||
<application
|
||||
android:name=".MyApp"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
@ -25,6 +26,8 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".view.MapsActivity"></activity>
|
||||
<!--
|
||||
The API key for Google Maps-based APIs is defined as a string resource.
|
||||
(See the file "res/values/google_maps_api.xml").
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency;
|
||||
|
||||
import android.location.Address;
|
||||
|
||||
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
|
||||
public class MapMarker {
|
||||
|
||||
private Address address;
|
||||
private LatLng latLng;
|
||||
private String title;
|
||||
private float color;
|
||||
|
||||
public MapMarker(Address address, String title, float color) {
|
||||
this.address = address;
|
||||
this.latLng = new LatLng(this.address.getLatitude(), this.address.getLongitude());
|
||||
this.title = title;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public LatLng getLatLng() {
|
||||
return latLng;
|
||||
}
|
||||
|
||||
public void setLatLng(LatLng latLng) {
|
||||
this.latLng = latLng;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public float getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(float color) {
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
public class MyApp extends Application {
|
||||
private static MyApp instance;
|
||||
|
||||
public static MyApp getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static Context getContext(){
|
||||
return instance;
|
||||
// or return instance.getApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
instance = this;
|
||||
super.onCreate();
|
||||
}
|
||||
}
|
||||
@ -8,8 +8,11 @@ import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class MapsActivityRepository {
|
||||
public List<Address> get_lat_long_from_address(String address, Context context) throws IOException {
|
||||
|
||||
public static Address get_lat_long_from_address(String address, Context context) throws IOException {
|
||||
Geocoder gc = new Geocoder(context);
|
||||
return gc.getFromLocationName(address, 1);
|
||||
return gc.getFromLocationName(address, 1).get(0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
@ -32,6 +33,9 @@ final public class AgentsActivity extends AppCompatActivity implements OnClickLi
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
|
||||
viewModel = new ViewModelProvider(this).get(AgentsActivityViewModel.class);
|
||||
observeAgents();
|
||||
|
||||
Intent i = new Intent(AgentsActivity.this, MapsActivity.class);
|
||||
AgentsActivity.this.startActivity(i);
|
||||
}
|
||||
|
||||
private void observeAgents()
|
||||
|
||||
@ -1,33 +1,57 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.view;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import android.location.Address;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
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.OnMapReadyCallback;
|
||||
import com.google.android.gms.maps.SupportMapFragment;
|
||||
import com.google.android.gms.maps.model.BitmapDescriptor;
|
||||
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
||||
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 java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.MapMarker;
|
||||
import fr.romanet.vj.apps.myrealestateagency.R;
|
||||
import fr.romanet.vj.apps.myrealestateagency.repository.MapsActivityRepository;
|
||||
import fr.romanet.vj.apps.myrealestateagency.viewmodel.MapsActivityViewModel;
|
||||
//import fr.romanet.vj.apps.myrealestateagency.viewmodel.MainActivityViewModel;
|
||||
|
||||
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
|
||||
|
||||
private GoogleMap mMap;
|
||||
private MapsActivityViewModel viewModel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_maps);
|
||||
|
||||
|
||||
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
|
||||
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
|
||||
.findFragmentById(R.id.map);
|
||||
mapFragment.getMapAsync(this);
|
||||
|
||||
// - Define and point to the ViewModel
|
||||
viewModel = new ViewModelProvider(this).get(MapsActivityViewModel.class);
|
||||
|
||||
getLifecycle().addObserver(viewModel);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,13 +65,50 @@ public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
|
||||
*/
|
||||
@Override
|
||||
public void onMapReady(GoogleMap googleMap) {
|
||||
mMap = googleMap;
|
||||
LatLng latlng_cachan;
|
||||
LatLng latlng_efrei;
|
||||
|
||||
// Add a marker in Sydney and move the camera
|
||||
LatLng sydney = new LatLng(-34, 151);
|
||||
// mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
|
||||
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
|
||||
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
|
||||
// List<String> ll = MapsActivityRepository.get
|
||||
LatLngBounds latlngbnd;
|
||||
observeAddresses(googleMap);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void observeAddresses(GoogleMap mMap){
|
||||
viewModel.markers_to_display.observe(this, new Observer<List<MapMarker>>() {
|
||||
List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
|
||||
MarkerOptions tmp_marker;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<MapMarker> mapMarkers) {
|
||||
|
||||
for (MapMarker mapMarker: mapMarkers){
|
||||
tmp_marker = new MarkerOptions().position(mapMarker.getLatLng()).title(mapMarker.getTitle()).icon(BitmapDescriptorFactory.defaultMarker(mapMarker.getColor()));
|
||||
mMap.addMarker(tmp_marker);
|
||||
markers.add(tmp_marker);
|
||||
}
|
||||
|
||||
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
|
||||
@Override
|
||||
public void onMapLoaded() {
|
||||
LatLngBounds.Builder builder = new LatLngBounds.Builder();
|
||||
for (MarkerOptions marker : markers) {
|
||||
builder.include(marker.getPosition());
|
||||
}
|
||||
LatLngBounds bounds = builder.build();
|
||||
|
||||
int padding = 200; // offset from edges of the map in pixels
|
||||
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
|
||||
|
||||
mMap.animateCamera(cu);
|
||||
|
||||
|
||||
|
||||
// mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latlngbnd, 30));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
||||
|
||||
import android.app.Application;
|
||||
import android.location.Address;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
|
||||
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.MapMarker;
|
||||
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||
import fr.romanet.vj.apps.myrealestateagency.repository.MapsActivityRepository;
|
||||
import fr.romanet.vj.apps.myrealestateagency.view.MapsActivity;
|
||||
|
||||
public class MapsActivityViewModel extends AndroidViewModel implements LifecycleObserver {
|
||||
|
||||
public MapsActivityViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
}
|
||||
|
||||
public MutableLiveData<List<MapMarker>> markers_to_display = new MutableLiveData<>();
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
||||
private void init_map() throws IOException {
|
||||
List<MapMarker> mapMarkers = new ArrayList<MapMarker>();
|
||||
// List<LatLng> latLngs = null;
|
||||
|
||||
mapMarkers.add(new MapMarker(MapsActivityRepository.get_lat_long_from_address("Paris, France", MyApp.getContext()), "Paris", BitmapDescriptorFactory.HUE_BLUE));
|
||||
mapMarkers.add(new MapMarker(MapsActivityRepository.get_lat_long_from_address("Villejuif, France", MyApp.getContext()), "Villejuif", BitmapDescriptorFactory.HUE_BLUE));
|
||||
mapMarkers.add(new MapMarker(MapsActivityRepository.get_lat_long_from_address("Cachan, France", MyApp.getContext()), "Cachan", BitmapDescriptorFactory.HUE_BLUE));
|
||||
mapMarkers.add(new MapMarker(MapsActivityRepository.get_lat_long_from_address("Massy, France", MyApp.getContext()), "Massy", BitmapDescriptorFactory.HUE_GREEN));
|
||||
mapMarkers.add(new MapMarker(MapsActivityRepository.get_lat_long_from_address("Ivry, France", MyApp.getContext()), "Ivry", BitmapDescriptorFactory.HUE_RED));
|
||||
|
||||
// for (MapMarker mapMarker : mapMarkers){
|
||||
// latLngs.add(new LatLng(addr.getLatitude(), addr.getLongitude()));
|
||||
// }
|
||||
|
||||
markers_to_display.postValue(mapMarkers);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user