Google Maps API v2 invoke behaviour of clicking mylocationbutton - android

I have a fully working GoogleMap and I can show my userlocation with the call
mMap.setMyLocationEnabled(true);
What I want to accomplish is the functionality of the actual click on the button that now appears on the GoogleMap (I.e animating the map to the userlocation with correct zoom etc).
The functionality is there in the GoogleMap-object already, how do I use it?
I do not want to use a LocationListener or such to accomplish this, I just want to "call the same code" that gets called when I click the button on the map. Can it be that simple?
Thanks in advance.
EDIT:
What I basically want to do is to center the map at the user location, exactly the way that the GoogleMap centers at user location when I click the button. Like this:
GoogleMap mMap = this.getMap();
if(mMap != null) {
mMap.setMyLocationEnabled(true);
//TODO center the map on mylocation
}
EDIT:
Apparently Google is working on this. http://code.google.com/p/gmaps-api-issues/issues/detail?id=4644

This is how I do to navigate to the center of the map when we get the first location-update.
my class header:
public class FragActivity extends SherlockFragmentActivity implements OnMyLocationChangeListener
private GoogleMap mMap;
my mMap-setup:
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = customMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
setUpMap-method:
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
and my onlocationchange:
#Override
public void onMyLocationChange(Location lastKnownLocation) {
CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude())).zoom(6).build());
mMap.moveCamera(myLoc);
mMap.setOnMyLocationChangeListener(null);
}
Works like a charm

Not really sure why the other answers are talking about OnMyLocationChangeListeners. This is not what was asked in the OP. What was asked was how to replicate the exact behavior of clicking on the MyLocationButton, which is actually more complex than just animating the camera to the user's location. The button does different things depending on what zoom level you're currently on.
For example, when you're zoomed way out, clicking the button will animate you to your location while zooming you in, whereas if you're already at certain zoom levels, clicking the button will only animate you to your location while maintaining the current zoom level.
Rather than painstakingly trying to replicate this behavior, wouldn't it be great if we could simply get a reference to the MyLocationButton and just, you know, click it? Well, this is the current best way I've found to do just that:
#OnClick(R.id.fabMyLocation)
public void onMyLocationClick(FloatingActionButton fabMyLocation) {
View myLocationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).getParent())
.findViewById(Integer.parseInt("2"));
myLocationButton.performClick();
}
Yeah, it's not pretty, and relies on hardcoded IDs that may change in future versions of the Google Maps API. But this method of retrieving the button is based off of this highly rated answer from 2013 and so far, as of Google Play Services 8.4.0, still works flawlessly.

Update February 2013:
This is now possible. You can now set the map's OnMyLocationChangeListener. Receive an update as you want and move the camera accordingly.
This is not possible. I've found that the GoogleMap in Android is quite limited in the information that it gives you. It simply doesn't give you direct access to the user location updates or to the button that the user can press (you can only enable/disable it, not simulate a press).
You must implement the LocationSource and LocationListener interfaces and use a LocationManager to get updates from the device and use them to send them to the map.
You could then add your own custom button to the overlay to replicate the default button and use your own camera and zoom controls to simulate the functionality.
Source: hours of searching

Related

How to initialize a google map so that marker placed is visible in android [duplicate]

This question already has answers here:
Android Google maps API V2 center markers
(2 answers)
Closed 7 years ago.
I have recently deployed Google map API in my android application. The problem is that the marker placed on the map is not visible until user scroll/navigate over the view where map's fragment is deployed.
problem is" How to focus the Google map to the marker of a specific location on initializing.
EDITED: 25 AUGUST 2017
I have placed the marker for a particular location using following this blog post
PROBLEM
I am trying to view my marker directly when a user sees the map. Not by scrolling the map to see the marker.
You need to animate or move the map to the place where the marker is placed. I have pasted a snippet which should help you achieve that. Implement OnMapReadyCallback interfere and once the map is ready you should get a callback thru onMapReady(). This code only for your reference. You can place the marker related code wherever you need and works fine.
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng location = new LatLng(<latitude>, <longitude>);
CameraPosition target = CameraPosition.builder().target(location).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(target), 5000, null);
googleMap.addMarker(new MarkerOptions().position(location));
}

Does Google Maps Android API locate me automatically?

I am working with Google Maps Android API V2.
When my map fragment is loaded it shows my location automatically (with the blue dot). Why does this happen? Is my location history affecting or is the default working mode?
Does this code cause it?:
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
googleMap.setOnMyLocationChangeListener(null);
}
});
I have commented this piece of code but the map is still showing the blue dot.
If this is not happening because of the history, would this piece of code be enough to get user location or do I need to use LocationManager class as described in other questions?
Try to use
googleMap.setMyLocationEnabled(true/false);
to control showing your location on map. I think it is enough to turn the setting on to make your location work with Google Maps, without need to use LocationManager etc.

Android markers disapear after recompiling the application

Hi I am working on google maps and more specifically on setting some markers when the user clicks on the map. The problem is that whenever I re-run the activity or re-compile the application the markers disappear from the map. Any advice on that. Please help. Thank you
Here is my code where the markers are created on the onmapclick event.
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng position) {
gMap.addMarker(new MarkerOptions().position(position)
.title("BAR")
.snippet("DRINKS")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
});
}
Think of Markers as you think of of Android Views. If you re-run the application, user and password EditTexts that were previously entered are not filled anymore.
Use some kind of persistance to achive it. Check out your storage options here.

Adding a custom OnMarkerDragListener makes markers no longer draggable

I'm using Google Maps v2 in an Android application that I'm writing. After creating the map and adding all the markers to it, I make all the markers draggable by using marker.setDraggable(true); for each marker on the map.
If I run the code like this, the markers are indeed draggable. However, I'd like to change the icon or color of the marker while it's being dragged, so I set up an OnMarkerDragListener, like so:
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
#Override
public void onMarkerDragStart(Marker marker) {
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
}
#Override
public void onMarkerDrag(Marker marker) {
}
#Override
public void onMarkerDragEnd(Marker marker) {
marker.setIcon(BitmapDescriptorFactory.defaultMarker());
}
});
After adding in that block of code, though, the markers are no longer draggable. The color successfully changes in both the onMarkerDragStart and onMarkerDragEnd methods, but that's it. Can anyone explain what I'm doing wrong here, and why the markers aren't getting dragged ?
i would say that the problem comes from changing the icon rather than from settings the listener.
Please note that Marker.setIcon is not a function that is available in all the versions of the GoogleMaps V2 API. This means that probably it is not available in the library you have, or in the version of the play services installed in the device in which you are trying your app, and that could be causing any kind of malfunction.
As you can read here that function was added in May 2013. Please try updating everything, the google-play-services_lib, and the play services installed in the device.
If this happen to fix it, then you can try to change the icon only depending on the version installed on the mobile.

exist some method for handle the click on predefined location button in google maps Api 2?

http://www.flickr.com/photos/95897065#N03/8758730915/
exist some method for handle clicks in this button?
Not as of rev.7 of API v2. Hopefully it will be added in the near future. Feature request for this exists on gmaps-api-issues.
Edit:
This is now available via
map.setOnMyLocationClickListener(...);
You can get that button to be shown by doing something like this:
private GoogleMap map;
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true); // !!! important part

Categories

Resources