0

I have used the following code and I am passing Latitude and Longitude from Emulator Gps.

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;


public class MainActivity extends Activity implements
    GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

GoogleApiClient mGoogleApiClient;
Location mLastLocation;
TextView tvLatlong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvLatlong = (TextView) findViewById(R.id.tvLatlong);

    buildGoogleApiClient();

    if(mGoogleApiClient!= null){
        mGoogleApiClient.connect();
    }
    else
        Toast.makeText(this, "Not connected...", Toast.LENGTH_SHORT).show();


}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    Toast.makeText(this, "Failed to connect...", Toast.LENGTH_SHORT).show();

}

@Override
public void onConnected(Bundle bundle) {

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);

    if (mLastLocation != null) {
        tvLatlong.setText("Latitude: "+ String.valueOf(mLastLocation.getLatitude())+"Longitude: "+
                String.valueOf(mLastLocation.getLongitude()));
    }

}

@Override
public void onConnectionSuspended(int arg0) {
    Toast.makeText(this, "Connection suspended...", Toast.LENGTH_SHORT).show();

}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
 }
}

But i am not getting anything on the screen. But in Log i am getting following stuff.

08-20 03:16:54.107  22302-22302/? D/dalvikvm﹕ Late-enabling CheckJNI
08-20 03:16:54.195  22302-22308/? E/jdwp﹕ Failed sending reply to debugger:   Broken pipe
08-20 03:16:54.195  22302-22308/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
08-20 03:16:54.231  22302-22302/? I/dalvikvm﹕ Could not find method  android.app.Notification$Builder.setLocalOnly, referenced from method  com.google.android.gms.common.GooglePlayServicesUtil.zza
08-20 03:16:54.231  22302-22302/? W/dalvikvm﹕ VFY: unable to resolve virtual method 238: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
08-20 03:16:54.231  22302-22302/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x00c2
08-20 03:16:54.231  22302-22302/? I/dalvikvm﹕ Could not find method android.app.AppOpsManager.checkPackage, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
08-20 03:16:54.231  22302-22302/? W/dalvikvm﹕ VFY: unable to resolve virtual method 116: Landroid/app/AppOpsManager;.checkPackage (ILjava/lang/String;)V
08-20 03:16:54.231  22302-22302/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0010
08-20 03:16:54.231  22302-22302/? I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
08-20 03:16:54.231  22302-22302/? W/dalvikvm﹕ VFY: unable to resolve virtual method 533: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
08-20 03:16:54.231  22302-22302/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000b
08-20 03:16:54.307  22302-22302/? D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
08-20 03:16:54.323  22302-22302/? D/﹕ HostConnection::get() New Host Connection established 0xb79c8b28, tid 22302
08-20 03:16:54.375  22302-22302/? D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
08-20 03:16:54.375  22302-22302/? D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
08-20 03:16:54.523  22302-22302/com.doshaheen.www.myapplication W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
08-20 03:16:54.523  22302-22302/com.doshaheen.www.myapplication E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
08-20 03:16:54.543  22302-22302/com.doshaheen.www.myapplication E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-20 03:16:54.543  22302-22302/com.doshaheen.www.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0

Ide Used:Android Studio

Emulator used:Genymotion (Galaxy note 2 4.3 api 18)

Dependencies used:

com.android.support:appcompat-v7:23.0.0

com.google.android.gms:play-services:7.8.0'

AGM
  • 83
  • 1
  • 2
  • 12
  • Have you added permissions in the manifest for coarse and fine location? – Domain Aug 20 '15 at 07:40
  • You could refer to this answer which uses a separate GPSTracker Class to obtain GPS.http://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android – Domain Aug 20 '15 at 09:30

0 Answers0