Implement Google Map in Android Studio
Add the Api key to your app:-
Add the following code to the local.properties file in your project's root directory. Your API key should be replaced with YOUR API KEY.
MAPS_API_KEY=ADD_YOUR_API_KEY
Save the file and use Gradle to sync your project.
The next step is to add various permissions to the AndroidManifest.XML file, as well as the Google Map API key. Its syntax is as follows:
XML layout file:-
The XML layout file activity map.xml determines the structure of the app's user interface.
The file may be found in the res/layout folder. The activity map.xml file defines a fragment with the following elements: Sets the fragment's class name to SupportMapFragment, which corresponds to the fragment type used in the maps activity file.
You must now insert the map fragment You must now insert the map fragment into the xml layout file. The syntax is as follows:nto the xml layout file.
You must now insert the map fragment into the xml layout file. The syntax is as follows:
Maps activityfile:-
The maps activity file is the app's main activity, including the code for managing and displaying the map.
The SupportMapFragmentobject is the parent element of the app's UI and handles the map's life cycle.
private var currentLocationMap: FragmentContainerView? = null //google mapprivate lateinit var googleMaps: GoogleMap
Changing Map Type:-
You can also modify the MAP's kind. There are four main sorts of maps, each of which provides a unique perspective on the map. Normal, Hybrid, Satellite, and Terrain are the four kinds. Youcan put them to use as follows:
private val MAP_TYPES = intArrayOf( GoogleMap.MAP_TYPE_SATELLITE, GoogleMap.MAP_TYPE_NORMAL, GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_TERRAIN, GoogleMap.MAP_TYPE_NONE
/*** Get the SupportMapFragment and receive notifications when the map is ready to use. */ @Throws(Exception::class) internal fun getMap() { val map = supportFragmentManager.findFragmentById(R.id.currentLocationMap) as SupportMapFragment?map? .getMapAsync(this) }
override fun onMapReady(googleMap: GoogleMap) { val circleDrawable = ContextCompat.getDrawable(this, R.drawable.ic_location_point) val markerIcon = getMarkerIconFromDrawable(circleDrawable) googleMaps = googleMapgoogleMaps.mapType = MAP_TYPES[1] val latLng = LatLng(28.7041, 77.1025) val marker = MarkerOptions().position(latLng).icon(markerIcon) googleMaps.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18F)) googleMaps.animateCamera(CameraUpdateFactory.zoomTo(18f))//drop marker from top to bottom with animation val markerAnimation = MyBounceInterpolator(latLng.latitude, latLng.longitude) markerAnimation.dropMarker(googleMaps.addMarker(marker), googleMaps) }
Animatemarkerinandroidmap:-
public class MyBounceInterpolator implements Interpolator { double mAmplitude = 1; double mFrequency = 10; public MyBounceInterpolator(double amplitude, double frequency) { mAmplitude = amplitude; mFrequency= frequency; } public float getInterpolation(float time) { double amplitude = mAmplitude; if (amplitude == 0) { amplitude = 0.05; } return (float) (-1 * Math.pow(Math.E, -time / mAmplitude) * Math.cos(mFrequency * time) + 1); } public void dropMarker(final Marker marker, GoogleMap map) { final LatLng finalPosition = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude); Projection projection = map.getProjection(); Point startPoint = projection.toScreenLocation(finalPosition); startPoint.y = 0; final LatLng startLatLng = projection.fromScreenLocation(startPoint); final Interpolator interpolator = new MyBounceInterpolator(0.11, 4.6); TypeEvaluator typeEvaluator = new TypeEvaluator () { @Override public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) { float t = interpolator.getInterpolation(fraction); double lng = t * finalPosition.longitude + (1 -t) * startLatLng.longitude; double lat = t * finalPosition.latitude + (1 -t) * startLatLng.latitude; return new LatLng(lat, lng); } }; Property property = Property.of(Marker.class, LatLng.class, "position"); ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition); animator.start(); } }