Android 14 business features for your distributed teams
- By Google
- VIP Lounge
- 0 Replies
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
public class tab1 extends Fragment implements GoogleMap.OnMarkerClickListener,OnMapReadyCallback{
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
MapView mMapView;
private GoogleMap googleMap;
LocationManager locationManager;
LocationListener locationListener;
private PersisDatabase db;
ArrayList<Listings> listings = new ArrayList<>();
private Executor executor = Executors.newSingleThreadExecutor();
boolean first_time_load = true;
boolean tab_first_time_load = true;
int LOCATION_PERMISSION_REQUEST_CODE = 1;
private UpdateListingsTable updateListings;
public tab1() {
// Required empty public constructor
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
Log.d("MyMap", "setUpMapIfNeeded");
mMapView.getMapAsync(this);
}
}
public void onMapReady(GoogleMap googleMap) {
Log.d("maps", "onMapReady");
googleMap = googleMap;
setUpMap();
}
private void setUpMap() {
googleMap.getUiSettings().setMapToolbarEnabled(false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("loading","onCreate");
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d("loading","onViewCreated");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("loading","onCreateView");
Log.d("maps","onCreateView");
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
// googleMap.clear();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d("maps","loaded after seconds");
firstLoadData();
Toast.makeText(getActivity(), getResources().getString(R.string.message_loading_map), Toast.LENGTH_SHORT).show();
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
Log.d("maps","loaded after seconds 10");
loadMarkers();
}
}, 2000);
}
}, 1000);
Log.d("maps","setOnMapLoadedCallback");
}
});
//UiSettings mapUiSettings = googleMap.getUiSettings();
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (first_time_load){
centerMapOnLocation(location, getResources().getString(R.string.yourLocation));
first_time_load = false;
Log.d("maps:","first_time_load:" + first_time_load);
}
//Log.d("maps:","location changed");
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Log.d("maps:","status changed" + s.toString());
}
@Override
public void onProviderEnabled(String s) {
Log.d("maps:","provider enabled");
}
@Override
public void onProviderDisabled(String s) {
Log.d("maps:","provider disabled");
}
};
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, getResources().getString(R.string.yourLocation));
} else {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
updateListings = UpdateListingsTable.getInstance(getContext());
updateListings.insertRecords();
mMapView.onResume();
Log.d("maps", "onResume");
setUpMapIfNeeded();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
public void centerMapOnLocation(Location location, String title) {
Log.d("maps:","centerMapOnLocation");
if (location != null) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
Log.d("maps","user loc lat lon:" + userLocation);
// googleMap.clear();
loadMarkers();
googleMap.addMarker(new MarkerOptions().position(userLocation)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)).title(title));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,6));
}else{ //location not available zoom on Tehran
LatLng tehranLocation = new LatLng(35.6892, 51.3890);
Log.d("maps","Tehran lat lon:" + tehranLocation);
// googleMap.clear();
loadMarkers();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tehranLocation,10));
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.d("maps:","onRequestPermissionsResult");
Log.d("maps:","code" + requestCode);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("maps","PERMISSION_GRANTED");
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d("maps","ACCESS_FINE_LOCATION");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, getResources().getString(R.string.yourLocation));
}
}
}
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(marker))
{
Log.d("mapLocation","clicked");
}
return false;
}
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
boolean not_first_time_showing_info_window;
public MarkerInfoWindowAdapter() {
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(final Marker marker) {
Log.d("maps","getInfoContents clicked");
Log.d("maps","getInfoContents item:" + marker.getSnippet());
View v = getLayoutInflater().inflate(R.layout.fragment_map, null);
ImageButton listingPhoto = (ImageButton) v.findViewById(R.id.listingPhoto);
TextView markerLabel = (TextView) v.findViewById(R.id.title);
markerLabel.setText(marker.getTitle());
TextView purposeLabel = (TextView) v.findViewById(R.id.purpose);
int id;
try {
id = Integer.parseInt(marker.getSnippet());
Log.d("maps","ID:"+ id);
purposeLabel.setText(listings.get(id).getPurpose());
} catch (NumberFormatException nfe) {
purposeLabel.setText("");
Log.d("maps","error id");
}
try {
id = Integer.parseInt(marker.getSnippet());
String url = UrlRepository.getPictureUrl() + listings.get(id).getFolder() + "/" + Utilities.generateThumbnail(listings.get(id).getPhoto());
if (not_first_time_showing_info_window) {
not_first_time_showing_info_window = false;
if (listings.get(id).getPhoto() != null && !listings.get(id).getPhoto().isEmpty()) {
Picasso.get().load(url).into(listingPhoto);
} else {
Picasso.get().load(R.drawable.home).into(listingPhoto);
}
Log.d("maps", "first time");
} else {
not_first_time_showing_info_window = true;
if (listings.get(id).getPhoto() != null && !listings.get(id).getPhoto().isEmpty()) {
Picasso.get().load(url).into(listingPhoto, new InfoWindowRefresher(marker));
} else {
Picasso.get().load(R.drawable.home).into(listingPhoto);
}
Log.d("maps", "NOT first time");
}
} catch (NumberFormatException nfe) {
Log.d("maps", "there was a problem loading");
Picasso.get().load(R.drawable.home).into(listingPhoto);
}
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Log.d("intent", "window clicked:" + marker.getId());
int myId = 0;
try {
myId = Integer.parseInt(marker.getSnippet());
Intent myIntent = new Intent(getContext(), ListingViewActivity.class);
myIntent.putExtra("id", listings.get(myId).getId());
getContext().startActivity(myIntent);
} catch (NumberFormatException nfe) {
}
}
});
return v;
}
}
private class InfoWindowRefresher implements Callback {
private Marker markerToRefresh;
private InfoWindowRefresher(Marker markerToRefresh) {
this.markerToRefresh = markerToRefresh;
}
@Override
public void onSuccess() {
markerToRefresh.showInfoWindow();
}
@Override
public void onError(Exception e) {
}
}
public void firstLoadData(){
db = PersisDatabase.getInstance(getContext());
executor.execute(new Runnable() {
@Override
public void run() {
List<Listings> list = db.dataItemDao().getListings();
for (Listings item : list) {
listings.add(item);
}
}
});
}
public void loadMarkers(){
// mMapView.onResume();
db = PersisDatabase.getInstance(getContext());
executor.execute(new Runnable() {
@Override
public void run() {
// listings.clear();
List<Listings> list = db.dataItemDao().getListings();
for (Listings item : list) {
listings.add(item);
}
}
});
Log.d("maps:", "listing size loadMarkers:" + listings.size());
for(int item= 0; item < listings.size(); item++) {
LatLng loc = new LatLng(listings.get(item).getLatitude(), listings.get(item).getLongitude());
googleMap.addMarker(new MarkerOptions()
.snippet(String.valueOf(item))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.position(loc)
.title(listings.get(item).getTitle()));
}
}
}