Open santy8151 opened 1 year ago
dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' }
import com.google.gson.annotations.SerializedName; import java.util.List;
public class GeocodingResponse {
@SerializedName("results")
private List
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public static class Result {
@SerializedName("geometry")
private Geometry geometry;
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
public static class Geometry {
@SerializedName("location")
private Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public static class Location {
@SerializedName("lat")
private double latitude;
@SerializedName("lng")
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
}
}
}
import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query;
public interface GeocodingService {
@GET("geocode/json")
Call
import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient { private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private EditText addressEditText;
private Button getCoordinatesButton;
private GeocodingService geocodingService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addressEditText = findViewById(R.id.addressEditText);
getCoordinatesButton = findViewById(R.id.getCoordinatesButton);
geocodingService = RetrofitClient.getClient("https://maps.googleapis.com/maps/api/").create(GeocodingService.class);
getCoordinatesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchCoordinates();
}
});
}
private void fetchCoordinates() {
String address = addressEditText.getText().toString();
String apiKey = "YOUR_API_KEY_HERE"; // Reemplaza con tu clave API
Call<GeocodingResponse> call = geocodingService.getCoordinates(address, apiKey);
call.enqueue(new Callback<GeocodingResponse>() {
@Override
public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {
if (response.isSuccessful()) {
GeocodingResponse.Location location = response.body().getResults().get(0).getGeometry().getLocation();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(MainActivity.this, "Lat: " + latitude + ", Lng: " + longitude, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Error fetching coordinates", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<GeocodingResponse> call, Throwable t) {
Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
En esta api utilizamos diferentes parámetros para tener con exactitud la ubicación