get google map current location android studio – Here, i have share with you how to get google map current location in android studio using google map API. Now a days there are many apps provide the user current location via google map api. Google map officially provide the API to see the user current location in android app or anything.
These days without Google Map we did not find the best routes. That’s why every developers and peoples are starts to using google maps. This is very simple process make an current location. Okay, let’s start our tutorial to get google map current location in android studio.
Through the code you can easily get google map current location in android studio platform. But that’s not a easy to do, we need to activate and communicate third party library files. After that you can easily get the user location in any type of apps.
Get Google Map Current Location Android Studio
1. First, Open Android Studio and create new project. The project name is i have enter Google Map, you give any name for the project then click Next. Then follow the on step procedure to create the new apps. If you are beginner on this sector, don’t worry within a month you can learn the android code.
2. Choose the Google Maps Activity then click Next.
3.Then the Activity Name & Layout Name is default so don’t change it.
4. Then click Finish, now your project is created. After creating project you will see the picture like below
I have select one line in this picture, you have just copy and paste that line in browser, its move on create the Google API and you must login the google account to get the API.
Then click Agree and continue. Hereafter its move on Create API key like this,
Now you have click Create API key. Hereafter the API key is created, like this,
You have click to restrict the key. then the credential tab is opened now you have save click to save.
Now copy your API key and paste from google_maps_api.xml file.
Replace the API key in YOUR_KEY_HERE place. Now run your project the default google map is successfully executed in your emulator like this is my output.
Its all default google map, its show the same location in all android devices. Hereafter we have develop the main code to get user current location in google map.
Let’s start, first we have implement the main file of MapsActivity.java. You have add some interface. (like below image)
After add the interface, you have see the red lines in the interface because we not implement any methods so just put your cursor in over the red line and press alt+enter and click on implement methods (like below image).
Note:When you see the red line in your code, in that time put the cursor in red line and click alt+enter then its automatically suggest answer so you have click the labels (like implement methods,import class)
The full code of MapsActivity.java
Here, i have include the full code mapsactivity.java file. so just copy the full code and paste from your mapsactivity.java file.
package com.example.vetri.googlemap; import android.location.Location; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, GoogleMap.OnMarkerDragListener, GoogleMap.OnMapLongClickListener, View.OnClickListener{ //Our Map private GoogleMap mMap; //To store longitude and latitude from map private double longitude; private double latitude; //Buttons private ImageButton buttonSave; private ImageButton buttonCurrent; private ImageButton buttonView; //Google ApiClient private GoogleApiClient googleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); //Initializing googleapi client googleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); //Initializing views and adding onclick listeners buttonSave = (ImageButton) findViewById(R.id.buttonSave); buttonCurrent = (ImageButton) findViewById(R.id.buttonCurrent); buttonView = (ImageButton) findViewById(R.id.buttonView); buttonSave.setOnClickListener(this); buttonCurrent.setOnClickListener(this); buttonView.setOnClickListener(this); } @Override protected void onStart() { googleApiClient.connect(); super.onStart(); } @Override protected void onStop() { googleApiClient.disconnect(); super.onStop(); } //Getting current location private void getCurrentLocation() { mMap.clear(); //Creating a location object Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); if (location != null) { //Getting longitude and latitude longitude = location.getLongitude(); latitude = location.getLatitude(); //moving the map to location moveMap(); } } //Function to move the map private void moveMap() { //String to display current latitude and longitude String msg = latitude + ", "+longitude; //Creating a LatLng Object to store Coordinates LatLng latLng = new LatLng(latitude, longitude); //Adding marker to map mMap.addMarker(new MarkerOptions() .position(latLng) //setting position .draggable(true) //Making the marker draggable .title("Current Location")); //Adding a title //Moving the camera mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); //Animating the camera mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); //Displaying current coordinates in toast Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng latLng = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(latLng).draggable(true)); mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.setOnMarkerDragListener(this); mMap.setOnMapLongClickListener(this); } @Override public void onConnected(Bundle bundle) { getCurrentLocation(); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onMapLongClick(LatLng latLng) { //Clearing all the markers mMap.clear(); //Adding a new marker to the current pressed position mMap.addMarker(new MarkerOptions() .position(latLng) .draggable(true)); } @Override public void onMarkerDragStart(Marker marker) { } @Override public void onMarkerDrag(Marker marker) { } @Override public void onMarkerDragEnd(Marker marker) { //Getting the coordinates latitude = marker.getPosition().latitude; longitude = marker.getPosition().longitude; //Moving the map moveMap(); } @Override public void onClick(View v) { if(v == buttonCurrent){ getCurrentLocation(); moveMap(); } } }
Now move on the activity_maps.xml and copy the following code.
Then finally move on the AndroidManifest.xml and add the following code.That’s all code. Now run your application. You will get the following output in your application. We earn More money online watching videos & earn $1.