Last active
April 19, 2017 18:47
-
-
Save alvareztech/ef8fdadc3d2fc380e331e8c193a8093a to your computer and use it in GitHub Desktop.
Mi punto en el mundo. Versión 1. Día 2. Curso de servicios de localización en Android. 2017.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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:gravity="center" | |
android:orientation="vertical" | |
tools:context="tech.alvarez.mipuntoenelmundo.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/latitud" /> | |
<TextView | |
android:id="@+id/latitudTextView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="24sp" | |
tools:text="Aquí viene la latitud" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/longitud" /> | |
<TextView | |
android:id="@+id/longitudTextView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="24sp" | |
tools:text="Aquí viene la longitud" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 25 | |
buildToolsVersion "25.0.2" | |
defaultConfig { | |
applicationId "tech.alvarez.mipuntoenelmundo" | |
minSdkVersion 15 | |
targetSdkVersion 25 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile 'com.android.support:appcompat-v7:25.3.1' | |
compile 'com.android.support.constraint:constraint-layout:1.0.2' | |
testCompile 'junit:junit:4.12' | |
compile 'com.google.android.gms:play-services-location:8.4.0' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.mipuntoenelmundo; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.TextView; | |
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 AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
private TextView latitudTextView; | |
private TextView longitudTextView; | |
private GoogleApiClient googleApiClient; | |
private Location ultimaUbicacion; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
latitudTextView = (TextView) findViewById(R.id.latitudTextView); | |
longitudTextView = (TextView) findViewById(R.id.longitudTextView); | |
googleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
ultimaUbicacion = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); | |
if (ultimaUbicacion != null) { | |
latitudTextView.setText(String.valueOf(ultimaUbicacion.getLatitude())); | |
longitudTextView.setText(String.valueOf(ultimaUbicacion.getLongitude())); | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
googleApiClient.connect(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (googleApiClient.isConnected()) { | |
googleApiClient.disconnect(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">Mi punto en el mundo</string> | |
<string name="latitud">Latitud</string> | |
<string name="longitud">Longitud</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment