Im working with google maps project now and im fetching my data from firebase ,im able to plot multiple markers with the help of all the kind developers in this site, one thing is when plotting those markers my markers title are the same , the "same" means that all my markers have the same Title please see my image
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private GoogleMap googleMap;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();
private ArrayList<String> Names = new ArrayList<>();
String Lat ,Lon,Names1;
double latitude , longitude;
#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);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Positions");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
// System.out.println(chidSnap.getKey()); //displays the key for the node
// System.out.println(chidSnap.child("Latitude").getValue());
// System.out.println( chidSnap.child("Longitude").getValue()); //gives the value for given keyname
//some latitude and logitude value
// System.out.println(latlngs); //gives the value for given keyname
Lat = String.valueOf(chidSnap.child("Latitude").getValue());
Lon = String.valueOf(chidSnap.child("Longitude").getValue());
Names1 = String.valueOf(chidSnap.getKey());
latitude= Double.parseDouble(Lat);
longitude= Double.parseDouble(Lon);
latlngs.add(new LatLng(latitude, longitude));
Names.add(Names1);
}
if(mMap != null) {
for (LatLng point : latlngs) {
options.position(point);
options.title(String.valueOf(Names));
options.snippet("someDesc");
mMap.addMarker(options);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// System.out.println(Names);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for (LatLng point : latlngs) {
options.position(point);
options.title(String.valueOf(Names));
options.snippet("someDesc");
googleMap.addMarker(options);
}
}
}
My MapsActivity above. What im expecting is one marker should be named to Lex B05 , and the other user should be amanda thank in advance for your help!..
That because you are passing same arrayList Names to all marker, Try below code while adding marker
for (int i=0;i< latlngs.size();i++) {
LatLng point=latlngs.get(i);
options.position(point);
options.title(Names.get(i));
options.snippet("someDesc");
mMap.addMarker(options);
}
Hope this will help!!
Related
Can you guys help me out.
My code shows same document when I click a marker.
I think there problem in my for loop.
I am creating a list of parking spots and when clicked i would show the details of Parking spots in modal bottom sheet
here is my code.
public class ParkingSpace extends AppCompatActivity implements OnMapReadyCallback{
private GoogleMap mMap;
Parking parking;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference parkingRef = db.collection("Parking Lots");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parking_space);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(#NonNull GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(16.4023,120.5960);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,14));
parkingRef.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){
parking = documentSnapshot.toObject(Parking.class);
parking.setDocumentId(documentSnapshot.getId());
String lat = parking.getLatitude();
String lon = parking.getLongitude();
String title = parking.getparkingName();
String owner = parking.getName();
String documentId= parking.getDocumentId();
double latd = Double.parseDouble(lat);
double lond = Double.parseDouble(lon);
LatLng location = new LatLng(latd,lond);
mMap.addMarker(new MarkerOptions().snippet("Owner: " +owner).position(location).title(title));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(#NonNull Marker marker) {
parking = documentSnapshot.toObject(Parking.class);
String path = documentSnapshot.getReference().getPath();
Toast.makeText(ParkingSpace.this, "Document ID:" +documentId, Toast.LENGTH_SHORT).show();
BottomSheet bottomSheet = new BottomSheet();
Bundle bundle = new Bundle();
bundle.putString("lat",lat);
bottomSheet.setArguments(bundle);
bottomSheet.show(getSupportFragmentManager(),"bottomsheet");
return false;
}
});
}
}
});
}
}
You are effectively using the same Parking instance ( parking ) for all markers because each iteration of the loop sets it to the value of the next query result. So by the time the marker click event occurs the parking instance is the last one processed (for all markers).
An alternate approach is to associate a parking instance with the newly added marker and get that instance in the onMarkerClick.
Something like this:
// ... all your code up to this line remains same ....
// Here the `addMarker` method returns the newly added Marker to which we can associate
// any object needed - so associate the "parking" instance...
Marker m = mMap.addMarker(new MarkerOptions().snippet("Owner: " +owner).position(location).title(title));
m.setTag(parking);
Then in your onMarkerClick replace:
parking = documentSnapshot.toObject(Parking.class);
with
// Use the associated Parking object (saved as a tag) to get needed info
Parking p = (Parking) marker.getTag();
if (p == null) {
// some other marker on map
return;
}
String documentId = p.getDocumentId();
String lat = p.getLatitude();
// and any other data I may have missed should come from "p".
Getting trouble of plotting my markers on maps , im able to fetch my Latitude and Longitude of my users from firebase and store it to my arraylist , my problem is how im i able to set my arraylist globally? here is my code .
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private GoogleMap googleMap;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();
private ArrayList<String> Names = new ArrayList<>();
String Lat ,Lon,Names1;
double latitude , longitude;
#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);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Positions");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
Lat = String.valueOf(chidSnap.child("Latitude").getValue());
Lon = String.valueOf(chidSnap.child("Longitude").getValue());
Names1 = String.valueOf(chidSnap.getKey());
latitude= Double.parseDouble(Lat);
longitude= Double.parseDouble(Lon);
latlngs.add(new LatLng(latitude, longitude));
Names.add(Names1);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// System.out.println(Names);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for (LatLng point : latlngs) {
options.position(point);
options.title("Users");
options.snippet("someDesc");
googleMap.addMarker(options);
}
}
}
I have no idea what is wrong with this thing , somehow my map is showing no marker and my list "latlngs" is showing "0.0,0.0"
I have no idea how to set my latlngs globally since it is inside the code of fetching the datas from firebase.
Query to Firebase is an asynchronous process. So, probably your onDataChange is executed after onMapReady. Try below:
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
....
}
if(mMap != null) {
for (LatLng point : latlngs) {
options.position(point);
options.title("Users");
options.snippet("someDesc");
mMap.addMarker(options);
}
}
}
Or call mapFragment.getMapAsync(MapsActivity.this); from inside onDataChange
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
When i am trying to add custom marker it throws NullPointerException,
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
Here is my map activity
public class GoogleMapActivity extends AppCompatActivity
{
double latitude, longitude;
GoogleMap map;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_map);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
private void getCurrentLocation()
{
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
//Getting longitude and latitude
longitude = location.getLongitude();
latitude = location.getLatitude();
moveMap();
}
}
private void moveMap()
{
LatLng latLng = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true)
.title("Current Location"));
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(15));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.googlemap, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
getCurrentLocation();
moveMap();
}
return super.onOptionsItemSelected(item);
}
}
How can i fix this problem?
private void getCurrentLocation()
{
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
moveMap(location);
}
}
private void moveMap(Location location)
{
longitude = location.getLongitude();
latitude = location.getLatitude();
LatLng latLng = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true)
.title("Current Location"));
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(15));
}
You don't have latitude and longitude in the scope of function you are trying to access in.
and you are also not implementing onMapReady() function. Seems like a lot of things are broken.
Example: https://github.com/googlemaps/android-samples/blob/master/tutorials/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java
public class MapsMarkerActivity extends AppCompatActivity
implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps);
// Get the SupportMapFragment and request notification
// when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map when it's available.
* The API invokes this callback when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user receives a prompt to install
* Play services inside the SupportMapFragment. The API invokes this method after the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I want to draw polylines between two points with using polylineoptions.addAll sending a parameter of ArrayList<LatLng>, but it doesn't show me the result.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private ArrayList<LatLng> arrayPoints = new ArrayList<>();
PolylineOptions polylineOptions=new PolylineOptions();
#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.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
LatLng point=new LatLng(-35,151);
LatLng point2=new LatLng(-40,151);
polylineOptions.color(Color.RED);
polylineOptions.width(3);
arrayPoints.add(point);
arrayPoints.add(point2);
polylineOptions.addAll(arrayPoints);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap)
{
}
}
Looks like you're not adding the polyline to the map
mMap.addPolyline(polylineOptions)
I'm having some issues using FragmentActivity and SupportMapFragment. The map's zoom is all wonky.
Here's the code:
public class GoogleMapActivity extends FragmentActivity{
Double longitude, latitude;
static LatLng koordinate;
GoogleMap supportMap;
String title, address;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_googlemaps);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMap = fm.getMap();
longitude = 13.597651720046997;
latitude = 45.22456639103469;
title = "Turistička zajednica Općine Vrsar";
address = "ul. Rade Končara 46";
koordinate = new LatLng(longitude, latitude);
Marker marker = supportMap.addMarker(new MarkerOptions().position(koordinate).title(title).snippet(address)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_star)));
//supportMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
supportMap.moveCamera(CameraUpdateFactory.newLatLngZoom(koordinate, 15));
supportMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
}}
For some reason, I get following thing in google maps, which is not zoomed enough:
http://imageshack.us/f/812/3yl.png/
When I click on the zoom, the map goes into this state:
http://imageshack.us/f/825/k8np.png/
I need the maps to zoom enough so that the street names are shown.This works just fine in my previous application, but I used regular MapFragment there. For this app, I need SupportMapFragment. Maybe that's causing this issue?
Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
OR
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
the maximum zoom allowed is 17 i guess.
i hope this must work...