makers are calling the same activity on google maps - java

I have a little problem with applying makers on Google maps to call activities.
I can successfully create the points and even call the activitys by clicking them, but the problem is that they are always calling the same activity no matter what I do.
Can someone help me?
Already tried to move the variables, already tried to move the celula01 variable to see makes a connection, but none of this gives any return.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap, mCel;
#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;
mCel = googleMap;
LatLng celula04 = new LatLng(-23.174601, -45.839513);
mCel.addMarker(new MarkerOptions().position(celula04).title("Célula da Maria"));
mCel.moveCamera(CameraUpdateFactory.newLatLng(celula04));
mCel.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, Celula02.class);
startActivity(intent);
return false;
}
});
LatLng celula01 = new LatLng(-23.173300, -45.821273);
mMap.addMarker(new MarkerOptions().position(celula01).title("Célula do Rafael"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(celula01));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, Celula01.class);
startActivity(intent);
return false;
}
});
}
}
Thanks.

public class MapsActivity extends FragmentActivity implements
OnMarkerClickListener,
OnMapReadyCallback {
private static final LatLng celula04 = new LatLng(-23.174601, -45.839513);
private static final LatLng celula01 = new LatLng(-23.173300, -45.821273);
private Marker mcelula04;
private Marker mcelula01;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marker_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/** Called when the map is ready. */
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Add some markers to the map, and add a data object to each marker.
mcelula04 = mMap.addMarker(new MarkerOptions()
.position(celula04)
.title("Célula da Maria");
mcelula04.setTag(1);
mcelula01 = mMap.addMarker(new MarkerOptions()
.position(celula01)
.title("Célula do Rafael");
mcelula01.setTag(2);
// Set a listener for marker click.
mMap.setOnMarkerClickListener(this);
}
/** Called when the user clicks a marker. */
#Override
public boolean onMarkerClick(final Marker marker) {
// Retrieve the data from the marker.
Integer click = (Integer) marker.getTag();
// Check if a click count was set, then display the click count.
if (click = 1) {
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, Celula02.class);
startActivity(intent);
}elseif(click = 2){
Intent intent = new Intent(MapsActivity.this, Celula01.class);
startActivity(intent);
}
// Return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}
}

Related

Save the LatLng values of Marker into your database

I'm new to coding and I'm currently stuck with how to save my map markers to my SQLite database. I have an existing database for my users and passwords. I'll attach my maps activity and database helper and my edit activity which creates the markers. Any help will be appreciated.
public class MapActivity2 extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final int EDIT_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map2);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
Intent edit = new Intent(MapActivity2.this, EditActivity.class);
edit.putExtra("location", latLng);
MapActivity2.this.startActivityForResult(edit, EDIT_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (EDIT_REQUEST) : {
if (resultCode == Activity.RESULT_OK) {
MarkerOptions markerOptions = data.getParcelableExtra("marker");
mMap.addMarker(markerOptions);
}
break;
}
}
}}
This is the EditActivity which adds the marker to the map.
public class EditActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
final LatLng latlng = (LatLng) getIntent().getParcelableExtra("location");
final EditText title = (EditText) findViewById(R.id.title);
Button boton = (Button) findViewById(R.id.save);
boton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
MarkerOptions marker = new MarkerOptions().position(latlng);
if (title.getText() != null) {
marker.title(title.getText().toString());
}
Intent resultIntent = new Intent();
resultIntent.putExtra("marker", marker);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
}

How can my android app google maps draw tracks of my movements with gps on google map sdk

I want a tracking line behind my location pointer which is moving to draw a it, so that we can see the route the person takes
enter image description here
public class Activity2 extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationListener locationListener;
private LocationManager locationManager;
private final long MIN_TIME = 1000; // 1 second
private final long MIN_DIST = 5; //1 meters
private LatLng latLng;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
com.example.employeetrackingstarter.databinding.ActivityMapsBinding binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 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);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_WIFI_STATE}, PackageManager.PERMISSION_GRANTED);
}
#Override
public void onMapReady(GoogleMap googleMap) {
locationListener = new LocationListener() {
#Override
public void onLocationChanged(#NonNull Location location) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
googleMap.clear();
googleMap.addMarker(new MarkerOptions().position(latLng).flat(true).title("My Position").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker5)));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
};
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
}
catch (SecurityException e){
e.printStackTrace();
}
}
Try to use polyline, and when your user change location, draw it. You can see video link. First position its place where you start and second place its you current location.

Android Studio Map setOnMarkerClickListener not working on a fragment

As you can read in the title I have a problem with the method setOnMarkerClickListener in the map which is in a fragment.
The markers appear but when I click on them, their title simply appears instead of the Toast that I show below.
I have tried different solutions but none have worked for me. I leave my code here. Any help is appreciated!
public class DashboardFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap mMap;
public LocationManager locationManager;
public LocationListener locationListener;
public LatLng userLatLong,mLatLong1,mLatLong2;
public boolean isNetworkEnabled = false;
public boolean isGPSEnabled = false;
public MarkerOptions mPosicion,m1,m2;
public Marker marcadorPosicion,marcador1,marcador2;
SupportMapFragment fragment;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map2);
fragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//REAL TIME POSITION NTW
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
userLatLong = new LatLng(location.getLatitude(), location.getLongitude());
marcadorPosicion.remove();
mPosicion=new MarkerOptions().position(userLatLong).title("Estas aqui");
marcadorPosicion = mMap.addMarker(mPosicion);
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLatLong));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLatLong, 15));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
//Marcadores
cargarMapa();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getActivity(), "Marker Clicked", Toast.LENGTH_SHORT).show();
return false;
}
});
}
public void cargarMapa(){
//Marcadores
//Metemos las coordenadas de los monumentos
mLatLong1= new LatLng(43.3233106, -3.1215395);//Foto_002
mLatLong2= new LatLng(43.3483518, -3.1189326);//Foto_00
m1=new MarkerOptions().position(mLatLong1).title("Foto_002").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));//Foto_002
marcador1 = mMap.addMarker(m1);
m2=new MarkerOptions().position(mLatLong2).title("Foto_005").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));//Foto_005
marcador2 = mMap.addMarker(m2);
}
This is caused due to the fact you are returning false on the onclicklistener. According to the documentation returning false makes the default option of opening the info window.
So You could do something like
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Foto_002") || marker.getTitle().equals("Foto_005")) {
Toast.makeText(getActivity(), "Marker Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
Problem solved. For some reason when performing the addLayerMap method to add a kml to the map, the onMarkerClick method gets totally blocked. If someone has a similar problem in the future, what I'm doing is creating a route with Polyline with their respective coordinate points.

G Maps Android Java, last known location marker color different from Marker for location changed,but showing same marker color on running app

//do focus on the last else block of the code and the OnLocationChanged method(which is inside of the
OnMapReady() method)
this else block(contains the code fo rthe lastknown location method) and the onLocationChanged method
contains the code for the location which is being changed (here by passing from the extended controls
avaialable with the emulator,ie the box opens on clicking the downmost option in the emulator.)
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
LocationManager locationManager;
LocationListener locationListener;
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(final GoogleMap googleMap) {
mMap = googleMap;
locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener=new LocationListener() {
#Override
public void onLocationChanged(Location location) {
mMap.clear();
LatLng place = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(place).title("Marker in Sydney").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
mMap.animateCamera(CameraUpdateFactory.zoomTo(3));
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
// Add a marker in Sydney and move the camera
if(Build.VERSION.SDK_INT<23)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}else
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
else
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
Location lastknownlocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng position=new LatLng(lastknownlocation.getLatitude(),lastknownlocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title("The Location is"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
}
}
Add the yellow icon to your second marker just like you did with the first one, like this:
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastknownlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng position = new LatLng(lastknownlocation.getLatitude(), lastknownlocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title("The Location is").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
Screenshot:
Hope this helps!

How to add Google Maps marker from Firebase data?

I'm trying to create a map that loads markers from Firebase.
I've divided the position code to 2 different code: location_left and location_right.
It means that a normal Marker line will look like this:
LatLng cod = new LatLng(location_left, location_right);
googleMap.addMarker(new MarkerOptions().position(cod).title(party_title));
But it doesn't work. The application crashes.
This is the code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String FIREBASE_URL="https://haerev.firebaseio.com/";
private Firebase firebaseRef;
private GoogleMap mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps2);
Firebase.setAndroidContext(this);
firebaseRef = new Firebase(FIREBASE_URL);
// 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(final GoogleMap googleMap) {
firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child("users").getChildren()) {
String rightLocation = child.child("location_right").getValue().toString();
String leftLocation = child.child("location_left").getValue().toString();
double location_left = Double.parseDouble(leftLocation);
double location_right = Double.parseDouble(rightLocation);
String party_title = child.child("party/party_title").getValue().toString();
LatLng cod = new LatLng(location_left, location_right);
googleMap.addMarker(new MarkerOptions().position(cod).title(party_title));
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
Remove this line:
private GoogleMap mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Categories