Maps V2 myLocation blue dot callback
One possible workaround might be drawing a Marker
(with a similar icon) on top of the My Location dot so you can receive the corresponding onMarkerClick()
callback. This would also require removing the marker and adding it to the new location everytime there's a location update event, which you can listen to by implementing OnMyLocationChangeListener
.
EDIT: the OnMyLocationChangeListener
interface is now deprecated, one should use instead the new LocationClient
and the associated LocationListener
.
So the relevant code could look something like this (I hadn't actually tested this):
public class DemoMapFragment extends SupportMapFragment implements OnMyLocationChangeListener, OnMarkerClickListener {
// Note that 'mMap' may be null if the Google Play services APK is not available.
private GoogleMap mMap;
private Marker myLocationMarker;
private static BitmapDescriptor markerIconBitmapDescriptor;
/* ... */
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded(); // Get a reference to the map
mMap.setMyLocationEnabled(true); // Enable the my-location layer
mMap.setOnMyLocationChangeListener(this);
mMap.setOnMarkerClickListener(this);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map:
// Load custom marker icon
markerIconBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.my_location_dot_icon);
// When the map is first loaded we need to add our marker on top of My Location dot
myLocationMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude()))
.icon(markerIconBitmapDescriptor));
// Set default zoom
mMap.moveCamera(CameraUpdateFactory.zoomTo(15f));
}
}
}
@Override
public void onMyLocationChange(Location location) {
// Remove the old marker object
myLocationMarker.remove();
// Add a new marker object at the new (My Location dot) location
myLocationMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(location().getLatitude(),location().getLongitude()))
.icon(markerIconBitmapDescriptor));
}
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(myLocationMarker)) {
/* My Location dot callback ... */
}
}
}
GoogleMap.getMyLocation() returns null
The following code should work for the last known location of the user
LocationManager service = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
Related Topics
Android Gridlayout with Dynamic Number of Columns Per Row
Android Webview Err_Unknown_Url_Scheme
Set Inputtype for an Edittext Programmatically
Open an Image Using Uri in Android's Default Gallery Image Viewer
Java.Lang.Classnotfoundexception on Working App
Referencing a String in a String Array Resource with Xml
Android 4.3 Bluetooth Low Energy Unstable
How to Add a Fragment Inside a Viewpager Using Nested Fragment (Android 4.2)
Android Ics and Mjpeg Using Asynctask
Android Overriding Onbackpressed()
How to Create and Read Log on Android Devices
Get Text from Web Page to String
Android Webview, How to Handle Redirects in App Instead of Opening a Browser
Is There Any Reason to Use the Support.V4 Library in Android
How to Set Up Android Emulator Proxy Settings