Cannot get markers from Firebase - java

I cannot get markers on my map from Firebase. I have tried a few examples I found but I can't seem to get it to work. I want all of the locations that I have stored in FireBase to be on one map.
Code:
public class AllJobsMapActivity extends FragmentActivity implements OnMapReadyCallback {
private DatabaseReference geoDatabase = FirebaseDatabase.getInstance().getReference("places");
GoogleMap mapFragment;
List<Marker> markerList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_job_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
geoDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child("places").getChildren()) {
String latitude = child.child("lattitude").getValue().toString();
String longitude = child.child("longitude").getValue().toString();
double Latitude = Double.parseDouble(latitude);
double Longitude = Double.parseDouble(longitude);
String name = child.child("name").getValue().toString();
LatLng cod = new LatLng(Latitude, Longitude);
googleMap.addMarker(new MarkerOptions().position(cod).title(name));
}
}
#Override
public void onCancelled(DatabaseError firebaseError) {
}
});
}
}
Firebase Tree:

Related

Android - Add a Marker to a Fragment Map from MapsActivity

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?
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.

cannot add titles over places in google maps?

I've put some red marks over some places but I can not put texts over every place so that a different name and number is protected
As an example I want to type above each tag :
Grup_num + Index Number
Code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setZoomControlsEnabled(true);
// Add a marker in Sydney and move the camera
// LatLng Muscat = new LatLng(23.5840481, 58.388295);
double[] myListl = {25.696042,25.696847,25.697214};
double[] myListr = {32.649797,32.649693,32.651163};
//25.696042,32.649797
//25.696847,32.649693
//25.697214,32.651163
for(int i=0; i <myListl.length; i++) {
// Add a marker in Sydney and move the camera
LatLng Muscat = new LatLng(myListl[i], myListr[i]);
mMap.addMarker(new MarkerOptions().position(Muscat).title("Grup_num"+i).icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_location_1))).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Muscat, 16));
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, latLng.latitude+","+latLng.longitude, Toast.LENGTH_SHORT).show();
mMap.clear();
mMap.addMarker(new MarkerOptions().position(latLng).title("MyLocation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_location_1))).showInfoWindow();
}
});
}
}

Info Windows in Google map doesn't show pictures- android studio

I've been trying to show some info whith info windows -in my app which uses google map- with InfoWindowAdapter but it just does'nt show any pictures so whats the problem?
here's my code for it
public class findonmap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Bundle recevier=getIntent().getExtras();
float a=recevier.getFloat("x");
float b=recevier.getFloat("y");
LatLng mycordinate = new LatLng(a,b);
mMap.addMarker(new MarkerOptions().position(mycordinate).icon(BitmapDescriptorFactory.
fromResource(R.drawable.marker2)));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
// ConstraintLayout layout=(ConstraintLayout)findViewById(R.id.indo)
View view=getLayoutInflater().inflate(R.layout.info,null);
return view;
}
});
int zoomLevel = 4; //This goes up to 21
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mycordinate, zoomLevel));
}}

See the marker on the map

Create model for my marker:
public class FirebaseMarker {
public String dob;
public String dod;
public String name;
public double latitude;
public double longitude;
public FirebaseMarker() {
}
public FirebaseMarker(String name, double latitude, double longitude, String dob, String dod) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.dob = dob;
this.dod = dod;
}
Also create getters and setters for all this parametrs, after this i created class MapActivity(this activity i open when press on item in my recyclerView)
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
ChildEventListener mChildEventListener;
DatabaseReference mProfileRef = FirebaseDatabase.getInstance().getReference("Profile");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
createMapView();
FirebaseMarker marker = new FirebaseMarker("Lincoln", -34.506081, 150.88104, "24/12/1940", "02/07/2016");
FirebaseMarker marker2 = new FirebaseMarker("Lincoln", -35.506081, 140.88104, "24/12/1940", "02/07/2016");
mProfileRef.push().setValue(marker);
mProfileRef.push().setValue(marker2);
}
private void createMapView() {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
LatLng wollongong = new LatLng(-34.404336, 150.881632);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(wollongong, 18));
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
addMarkersToMap(googleMap);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
}
#Override
public void onStop() {
if (mChildEventListener != null)
mProfileRef.removeEventListener(mChildEventListener);
super.onStop();
}
private void addMarkersToMap(GoogleMap googleMap) {
mChildEventListener = mProfileRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
FirebaseMarker marker = dataSnapshot.getValue(FirebaseMarker.class);
double latitude = marker.getLatitude();
double longitude = marker.getLongitude();
String name = marker.getName();
LatLng location = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(location).title(name));
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
But when i open the activity i see just a map, how i can see the marker?
You Define Variable
GoogleMap googleMap;
and in
public void onMapReady(GoogleMap googleMap)
they are the same
so what about try to rename it
so
GoogleMap MgoogleMap;
and edit onMapReady
public void onMapReady(GoogleMap googleMap) {
MgoogleMap =googleMap
addMarkersToMap(MgoogleMap );
}

Android Google Maps Route Between 2 Points

I'm trying to to display route between 2 point, but it seems that its tougher than I though, although there are enough posts on the subject.
This is the code I have
public class MainActivity extends FragmentActivity implements OnMapReadyCallback
{
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
Context context;
LatLng Current = new LatLng(55.0567842,-1.599155200000041);
LatLng Target = new LatLng(55.0239304,-1.5453314000000091);
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(Current);
boundsBuilder.include(Target);
LatLngBounds bounds = boundsBuilder.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(ContextCompat.getColor(context, R.color.colorPrimary));
polyOptions.width(10);
polyOptions.add(Current, Target);
}
Any help with this would be much appreciated.

Categories

Resources