" Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of finding Latitude & Longitude of your position.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
***Please check on physical device or on emulator but make sure to set your position.
*** Make sure Google Play Services is installed or not. (if not please install first)
*** Add dependency in gradle file like
Hi ... Dear All ..Today I am uploading a superb example of finding Latitude & Longitude of your position.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
***Please check on physical device or on emulator but make sure to set your position.
*** Make sure Google Play Services is installed or not. (if not please install first)
*** Add dependency in gradle file like
compile 'com.google.android.gms:play-services:8.3.0'
*** Add meta tag with application tag in manifest file like
<meta-data android:name="com.google.android.gms.version"android:value="@integer/google_play_services_version"/>**** Also add permission in manifest file like<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
A. Nothing in Layout file only one text view is there to show valuesxml code of activity_main.xml is<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.exam.ravi.locationbsex1.MainActivity"> <TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Ltitude and Longitude... "android:id="@+id/textLocation"android:textSize="25sp"/></RelativeLayout>B. Java Code of MainActivity.javapackage com.exam.ravi.locationbsex1; import android.location.Location; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener ,GoogleApiClient.ConnectionCallbacks { TextView dis; GoogleApiClient googleApiClient; int SERVICE_CODE = 99; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dis = (TextView) findViewById(R.id.textLocation); if(checkServices()) { clientInitialization(); } } private void clientInitialization() { googleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addOnConnectionFailedListener(this) .addConnectionCallbacks(this) .build(); } @Override protected void onStart() { super.onStart(); googleApiClient.connect(); } @Override protected void onStop() { super.onStop(); if(googleApiClient.isConnected()) googleApiClient.disconnect(); } private boolean checkServices() { int result= GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); if(result!= ConnectionResult.SUCCESS) { if(GooglePlayServicesUtil.isUserRecoverableError(result)) { GooglePlayServicesUtil.getErrorDialog(result,this,SERVICE_CODE).show(); } return false; } return true; } @Override public void onConnected(Bundle bundle) { Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); if(location!=null) dis.setText("Latitude = " + location.getLatitude()+ "Longitude" +location.getLongitude()); else dis.setText("Location Not Found"); } @Overridepublic void onConnectionSuspended(int i) { Toast.makeText(this, "Connection Susspended", Toast.LENGTH_SHORT).show(); } @Overridepublic void onConnectionFailed(ConnectionResult connectionResult) { Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show(); }}
No comments:
Post a Comment