title: button click show current location in google map
body: <p>Hi i am new in android developing. This is my scenario Button click show current location in Google Map fragment in my application.</p>
  
  <p>Now i did button click showing current location <code>lattitude</code> and <code>longitude</code>.</p>
  
  <p>this is my code:</p>
  
  <p><strong>activity_main.xml</strong> </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;Button
          android:id="@+id/button_location"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"
          android:layout_marginStart="13dp"
          android:text="Start" /&gt;
  
      &lt;TextView
          android:id="@+id/text_location"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:textSize="16dp"/&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.example.hello.sampleapp;
  
  import android.Manifest;
  import android.app.AlertDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.location.LocationManager;
  import android.provider.Settings;
  import android.support.v4.app.ActivityCompat;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  
  public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  
      private static final int REQUEST_LOCATION = 1;
  
      Button button;
      TextView textView;
      LocationManager locationManager;
      String lattitude,longitude;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
  
          textView = (TextView)findViewById(R.id.text_location);
          button = (Button)findViewById(R.id.button_location);
          button.setOnClickListener(this);
  
      }
  
      @Override
      public void onClick(View view) {
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
              buildAlertMessageNoGps();
  
          } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
              getLocation();
          }
      }
  
  
      private void getLocation() {
          if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                  != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission
                  (MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  
              ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
  
          } else {
              Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  
              Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  
              Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);
  
              if (location != null) {
                  double latti = location.getLatitude();
                  double longi = location.getLongitude();
                  lattitude = String.valueOf(latti);
                  longitude = String.valueOf(longi);
  
                  textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude
                          + "\n" + "Longitude = " + longitude);
  
              } else  if (location1 != null) {
                  double latti = location1.getLatitude();
                  double longi = location1.getLongitude();
                  lattitude = String.valueOf(latti);
                  longitude = String.valueOf(longi);
  
                  textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude
                          + "\n" + "Longitude = " + longitude);
  
  
              } else  if (location2 != null) {
                  double latti = location2.getLatitude();
                  double longi = location2.getLongitude();
                  lattitude = String.valueOf(latti);
                  longitude = String.valueOf(longi);
  
                  textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude
                          + "\n" + "Longitude = " + longitude);
                  String url = "https://iradivi.com/test/?latitude=" + lattitude +"&amp;longitude=" + longitude;
  
  
              }else{
  
                  Toast.makeText(this,"Unble to Trace your location",Toast.LENGTH_SHORT).show();
  
              }
          }
      }
  
      protected void buildAlertMessageNoGps() {
  
          final AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("Please Turn ON your GPS Connection")
                  .setCancelable(false)
                  .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      public void onClick(final DialogInterface dialog, final int id) {
                          startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                      }
                  })
                  .setNegativeButton("No", new DialogInterface.OnClickListener() {
                      public void onClick(final DialogInterface dialog, final int id) {
                          dialog.cancel();
                      }
                  });
          final AlertDialog alert = builder.create();
          alert.show();
      }
  
  }
  </code></pre>
  
  <p>sorry for the question, i don't know how to ask this.</p>