Call setLocationEnabled in another Button - java

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.

Related

How to clear location marker in Google Map

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()

How to save a tracking in real time with google map

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.

How to know Google map has loaded our current location by using google map api in android?

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);
}
}

Google Maps API v2 for Android using FEATURE_ACTION_BAR_OVERLAY

I was viewing a different question Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps) and as you can see he's using the requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); to make the map v2 use the full screen. I tried this but the my location button is partially covered by the action bar on mine unlike his. Does anyone know how to move it down slightly?
Thanks!
Unfortunately you don't have any control over this button. Obviously, you can search for this button id in android sources and change layout params manually from the code, but I definitely wouldn't do that.
This button is super easy to implement yourself. So I recommend to disable built-in "locate me" button and implement your own which will be positioned right below the action bar.
To disable it use:
googleMap.setMyLocationEnabled(false);
New button onClick method content:
googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(googleMap.getMyLocation().getLatitude(),
googleMap.getMyLocation().getLongitude())));
You can just get the view by its id and re-set the top margin Apparently the id is always 2, but this isn't documented and your app will break when/if the id ever changes. Like Pavel said, it's safer to make your own location button and wire it up.
Unfortunately, calling ActionBar.getHeight() in your onCreate will always return 0 since layout hasn't finished. The way to get the real heigh is by using a listener on the ViewTreeObserver:
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
int actionBarHeight = getActionBar().getHeight();
// TODO: set margin of views dependent on actionbar height
// remove the listener
mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}

Android MyLocationOverlay vs LocationManager/LocationListener

I am building an Android application that shows the user his/her current location (using MyLocationOverlay), and I am also using a LocationManager/LocationListener to get the user's location every 15 seconds, which is then used to query a web service and in return I plot points using an ItemizedOverlay.
This is all working with no problems.
Should I be using BOTH MyLocationOverlay as well as LocationManager/LocationListener, however? It seems redundant. What would you do? From what I can see, MyLocationOverlay uses the LocationManager in the background to constantly receive location updates, anyway.
Thanks!
The MyLocationOverlay does a little more such as adding the overlay to the map, showing the error radius and making it cool and blinky! There is nothing wrong with using it in conjunction with getting your own updates. In my app I decided not to use it because I wanted to change the way that it looked so that it was more like what you see in the Maps app where the MyLocationOverlay is an arrow that shows what direction you're facing. It's really a matter of preference.

Categories