android


Re-position MyLocation button inside Google Map fragment


In the Google Maps API for Android there is button located at the top-right corner, served to locate you inside the map.
I would like to know if it is possible to relocate this icon that is by default in the API of Google Maps for Android, because at the top I plan to add an EditText as if it were floating.
I can suggest 2 options.
However, I strongly advise you to use the first one - much more classic and simple:
Use Map Padding (recommended)
You can re-position any of the GoogleMap Controls by setting the Padding from the corners. In your case, I would set some padding from the top:
googleMap.setPadding(0, numTop, 0, 0); //numTop = padding of your choice
It would also change the center position of the Map Camera accordingly, which is great for that kind of use case (adding a title / other floating controls).
Disable the button and create one of your own (less recommended)
Disabling it will be easy:
googleMap.getUiSettings().setMyLocationButtonEnabled(false)
However, creating a new one would be trickier - mostly because it is harder to set a fully-functional one.
I would Create a FloatingActionButton that looks like the one on the Google Maps application (example).
You can get the Icon from here (called "My Location")
Color the Fab in Material Grey 300 (maybe 400, can't remember at the moment)
Define an onClick Event that would move the camera to the user's current location (you're going to have to use Location Service for this.
Sample:
//Acquire a reference to the system Location Manager
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//Acquire the user's location
Location selfLocation = locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
//Move the map to the user's location
LatLng selfLoc = new LatLng(selfLocation.getLatitude(), selfLocation.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(selfLoc, 15);
googleMap.moveCamera(update);
If you noticed, when you click the "My Location" button, it starts to track you and move the camera accordingly. In order to create that effect, you need to override googleMap.onCameraMove and googleMap.onCameraIdle, and code your app so whenever the camera is idle, the map will continue to follow the user, and whenever the user moved the camera, it will stop.
Sample for onCameraIdle:
//Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
//Acquire the user's location
Location selfLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
LatLng cameraLocation = googleMap.getCameraPosition().target;
float[] results = new float[3];
Location.distanceBetween(selfLocation.getLatitude(), selfLocation.getLongitude(), cameraLocation.latitude, cameraLocation.longitude, results);
if (results[0] < 30) //30 Meters, you can change that
googleMap.moveCamera(...) //Move the camera to user's location

Related Links

Creating Live Wallpapers in Android
Changes not appearing when modifying Android system apps
Handling notifications in android
Parsing images from XML <enclosure>
Android onBackPress issue activity switching
Storing session cookie in android.webkit.CookieManager
Android : push notification automatic
Check both buttons BackgroundResources are same for button
How to programmatically add calendar subscriptions on Android?
Error in Manifest.xml when adding PACKAGE_USAGE_STATS [Android Lollipop]
i have a list view that contain two views textview and edittext. how i can get entire edittext data after pressing a single button
Is there a performance impact using android:numColumns=“auto_fit” in a GridView?
Java resource files are missing when I build my android library with gradle and proguard
Textview Gravity not working inside scrollview
How to make viewFlipper like this
comparing phone number from my contact list and an array of numbers

Categories

HOME
search
cheerio
github-for-windows
error-handling
rendering
yandex
spring-data-mongodb
cublas
telegram-bot
banner
barcode
accessibility
fabric.io
vimdiff
mel
heap
speech-synthesis
atlassian
searchview
mathprog
fileserver
google-content-api
enterprise-miner
exploit
tosca
jquery-callback
onload
syntax-highlighting
oculus
browser-sync
calculation
userdefaults
event-flow
volatile
hibernate-validator
r-grid
rselenium
hreflang
carriage-return
sharding
playframework-2.1
reactivekit
nohup
amazon-ses
node-mssql
protein-database
sim-card
bc
odroid
metaprogramming
kill
linqpad
bindingnavigator
photos
clang++
stylecop
textsum
magento-2.0.7
activeperl
css-transforms
oozie-coordinator
type-theory
py2neo
gameplay-kit
baidu
donations
webjars
delta
jtwig
nservicebus5
nss
assertion
fiware-monitoring
phonertc
atoi
osc
boost-program-options
formview
appserver
device-tree
android-framework
vbe
clickbank
qscrollarea
multitasking
ie8-compatibility-mode
cocoalibspotify-2.0
unit-of-work
beaker-testing
flex++
jquery-1.9
web-notifications
boost-msm
adobe-media-server
fusefabric
infobright
multiplatform
cloudfiles
photolibrary
vs-android
jqueryform
interprocess
folding
scrollbars
wcf-instancing
feasibility
mathematical-notation
visualj#
flvplayback

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App