The idea is to create an ArrayList of locations and every time I save the location data in that list in the same time I draw a polyline between each two points.
My question is about the Google Maps method onLocationChanged(). I want to know if it is called automatically, I mean every time the location change, or if it is called when the user click on the button of get current location?
If someone has a better idea please let me know?
Basically onLocationChanged is being called everytime the location is changed.
However it's bad idea to implement your code inside it, you can do something like that
public class GPSTracker implements LocationListener{
private Location location ;
public Location getLocation(){
return this.location;
}
// .. code
#Override
public void onLocationChanged(Location location) {
this.location=location;
}
}
now in your main activity you can get location by making an object of GPSTracker
then like this
GPSTracker=new GPSTracker(this);
gpsTracker.getLocation (); //this will provide you with longitude,latitude then you can control it with a thread or something like that, by calling getLongitude,getLatitude you can get the location paramter
be noted the constructor takes context as a parameter so pass the context of your activity to the GPSTracker class
please check the following link
As you've not provided your code, I don't know that you have a separate class for getting Location or in the same activity/class.
I am assuming that you want to insert your updated location in an Arraylist and want to create a polyline connection all those points. if I'm right, Here's what you can do.
Create an Arraylist global variable in your Activity
ArrayList<LatLng> cordList = new ArrayList<>();
Create a function/method call in onLocationChanged()
locationMarkers(location);
//passing updated location to the function/method.
(If you have a separate class for getting location then get the reference of your activity in it and call this method using that reference.)
Then create this method in your activity.
public void locationMarkers(Location location){
cordList.add(new LatLng(location.getLatitude(), location.getLongitude()));
Polyline line =googleMap.addPolyline(newPolylineOptions().width(6).color(Color.GREEN));
line.setPoints(cordList);
}
This way you'll have a polyline on the map of your locations. Also this polyline will be a straight line between two markers/positions. If you want it to be on the road, use HashMap.
Related
I am creating a map application using GoogleMap. I have a problem while moving a CustomMarker from first to second location with help of location change listener. CustomMarker gets duplicated because its maintaining the older position as well while updating to new location. Please refer the below image.
Duplication of single custom marker
Why won't you just clear() GoogleMap in order to remove old marker?
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#clear()
setLocationEnabled is a attribute that shows a button on the top of the map that makes the map go to the current place.
But I want to "hide" this Button and call the method on the another custom button. Is there a way? I don't wanna to create a big code to go to current location, it's not fast. I just wanna a button with the same function of the setLocationEnabled(true).
How do I do that?
I've dealt with a similar problem and I found a pretty detailed solution from here Google map for android my location custom button
Have fun!
As you may know, you can hide the current location button with the following:
UiSettings.setMyLocationButtonEnabled(false);
Now, if you want to perform this task manually, you'll have to do some work on your own. First, you will need to acquire the user's current location. This can be acquired (and updated) by overriding the onLocationChanged function of the LocationListener implementation. If you are unclear on how to do this, checkout the documentation describing it. This may prove useful as well!
Secondly, you'll need to target the user's location by setting the map's camera to look at it, and (possibly) set a marker in the location. Here's the function I used in one of my recent apps describing how to do so:
public void moveToLocation(Location input) {
Double lat = (double) (input.getLatitude());
Double lon = (double) (input.getLongitude());
final LatLng location = new LatLng(lat, lon);
setLocMarker(location);
mainMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16));
//if you want the camera to have an "animated" effect, you can perform the following
//mainMap.animateCamera(CameraUpdateFactory.zoomTo(16), 4000, null);
}
For more information regarding camera effects and animations, refer to this documentation.
I have created an application where in oncreate i am calling a function overlay(), which puts an overlay above the application. Then there are multiple screens that user can browse. My problem is I want when user Goes back to the Main screen that function should be again called and that overlay can be seen.Is there something i can use?
Try adding this code in your mainActivity
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
overlay();
}
I am working on app which needs geotagging and i want that whenever i press a button a new fragment opens which loads the google map with the current location and a screenshot is taken when the current location is loaded.
I have got success in taking screenshot and opening map in another fragment but i just want a way to know when our current location is loaded by google map and when it has loaded take the screenshot of it store it in sd card.
I have used How to programmatically take a screenshot in Android? for screenshot capture
I think you're looking for this callback
com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback
this is fired when
Callback interface for when the map has finished rendering. This occurs after all tiles required to render the map have been fetched, and all labeling is complete. This event will not fire if the map never loads due to connectivity issues, or if the map is continuously changing and never completes loading due to the user constantly interacting with the map.
Emphasis mine.
You can implement the callback and inside you call the code that takes the screenhsot.
If you need to do something after the map is loaded, and you want to know when that happens, you can implement a custom callback interface that you can manually fire when you are done with whatever it is you need to do before taking the screenshot.
You have to use the OnMapLoadedCallback listener.
OnMapLoadedCallback doesn't fire until after the tiles on map are loaded.
When you have a reference to the map set the call back.
mMyMap.setOnMapLoadedCallback(this);
When the onMapLoaded event fires take the snapshot.
#Override
public void onMapLoaded() {
if (mMyMap!= null) {
mMyMap.getScreenShot(this);
}
}
I am trying to create my first app in android studio, on the main screen there are three tick boxes asking the user which number of sides they want on the dice. I have a variable called sides which is set to 6,8 or 12 depending on which tick box the user ticks. I want the variable "sides" on the second activity so it can be used to generate a random integer between one and whatever "sides" is set to.
In first activity Lets assume that you have button GO . When You clicks on Button GO it should start Second Activity say Activity2.
Add following code to onClick of GO Button
Intent act2=new Intent(this,Activity2.class);//"this" is activity reference
act2.putExtra("key",value);
startActivity(act2);
Now in the onCreate method of Activity2 you can retrieve value of key as follows:
Int key=getIntent().getIntExtra("key",0);//0 is default value
In the same way as done above you can pass value of "side" variable to next activity
You can also save it in the Internal Storage and load it when you need, it is very useful because that way you can load it in every activity and every class you want.
you can learn how here.
I recommend watching all three parts.
Easiest way is to use singleton classes.
public class DataHolder {
public int sides = 0;
private static DataHolder dataHolder = new DataHolder();
public static DataHolder getInstance()
{
return dataHolder;
}
}
DataHolder.getInstance().sides=sideInActivityA;
you can access the variable by using
int sideInActivityB = DataHolder.getInstance().sides;