Google Map for Android My Location Custom Button

Google map for android my location custom button

See below xml file to custom button:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
android:id="@+id/maps"
android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" >

<ImageView
android:id="@+id/imgMyLocation"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="fitXY"
android:src="@drawable/track_my_location" />
</LinearLayout>

</RelativeLayout>

Then in java class, declare your location button:

private ImageView imgMyLocation;
imgMyLocation = (ImageView) findViewById(R.id.imgMyLocation);

Click Event:

imgMyLocation.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
getMyLocation();

}

Get Location Method, in that just pass your current latitude and longitude.

private void getMyLocation() {
LatLng latLng = new LatLng(Double.parseDouble(getLatitude()), Double.parseDouble(getLongitude()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
googleMap.animateCamera(cameraUpdate);
}
});

Set custom view for my location button in google map

The location button is actually an ImageView. You should first retrieve the fragment view that is associated with the map:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);

Next, you should get location view reference from the fragment:

ImageView locationButton = (ImageView) mapFragment.getView().findViewById(2);

Then change it's image to anything you want. e.g.

locationButton.setImageResource(R.drawable.icon_location);

Hope it helps.

How to change the position of My Location Button in Google Maps using android studio

you can use this

View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
rlp.setMargins(0, 180, 180, 0);

Styling google maps my location button in Android

You need to modify the map's camera as follows,

map.moveCamera(
CameraUpdateFactory.newLatLngZoom(location, DEFAULT_ZOOM.toFloat()))

to zoom into user position.
Read this for more details

How to create a custom Get Location button for a Google Maps App and conduct a search?

Answer 1 - I have made a custom button image and wanted to replace the default button with my own. Is that possible, and if so, what is the best way of going about that?

Yes. You can change default image to your own custom image.
Put address that saved your image like this pattern "@[package:]drawable/drawable_resource" to android:src= in <ImageView>.

<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/myimage" />

Then you should declare your location button, event, method.
If you need more specific information, refer to this Google map for android my location custom button.

In addition, if you need information about custom control, refer to this.

Answer 2 - I require my App to conduct a search upon opening using Taxis as the search criteria, is this also possible?

When you calculate directions, you may specify the transportation mode to use. By default, directions are calculated as driving directions. Refer to this Travel Modes.

You can access additional information for the VehicleType objects as described below.
<VehicleType.SHARE_TAXI>: Share taxi is a kind of bus with the ability to drop off and pick up passengers anywhere on its route.

If you need more specific, Refer to this How to specify “VehicleType”.

Adding a My Location Button in google maps on my android app

Use this to display the default location button on the Google Maps V2 API for android.

googleMap.setMyLocationEnabled(true); 

googleMap.getUiSettings().setMyLocationButtonEnabled(true);

how to change google map API's My Location button postion in flutter?

I found the solution. I create FAB button to change My Location button

GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
myLocationEnabled: true,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _currentLocation,
label: Text('My Location'),
icon: Icon(Icons.location_on),
),
);
}

also, set the _currentLocation method to call my location

void _currentLocation() async {
final GoogleMapController controller = await _controller.future;
LocationData currentLocation;
var location = new Location();
try {
currentLocation = await location.getLocation();
} on Exception {
currentLocation = null;
}

controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
zoom: 17.0,
),
));
}


Related Topics



Leave a reply



Submit