Skip to main content

Make a geocode request

Note

This example is a part of the Mapbox Android Demo app. You can find the values for all referenced resources in the res directory. For example, see res/values/activity_strings.xml for R.string.* references used in this example.

Geocoding API pricing
Products used within the Mapbox Java SDK, including the Geocoding API, are individually billed. For pricing information, see the documentation for the Geocoding API.
activity_javaservices_geocoding
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.javaservices.GeocodingActivity">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="-12.0463"
mapbox:mapbox_cameraTargetLng="-77.0427"
mapbox:mapbox_cameraZoom="11" />

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp">

<Button
android:id="@+id/choose_city_spinner_button"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_weight="1.25"
android:background="@color/mapboxBlue"
android:gravity="center"
android:padding="8dp"
android:text="@string/choose_city_spinner"
android:textAllCaps="false"
android:textColor="@android:color/white"
mapbox:layout_constraintBottom_toBottomOf="@+id/start_geocode_button"
mapbox:layout_constraintStart_toEndOf="@+id/start_geocode_button"
mapbox:layout_constraintTop_toBottomOf="@+id/geocode_latitude_editText" />

<EditText
android:id="@+id/geocode_latitude_editText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:hint="@string/latitude"
android:inputType="numberDecimal|numberSigned"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="parent" />


<EditText
android:id="@+id/geocode_longitude_editText"
android:layout_width="100dp"
android:layout_height="45dp"
android:hint="@string/longitude"
android:inputType="numberDecimal|numberSigned"
mapbox:layout_constraintLeft_toRightOf="@id/geocode_latitude_editText"
mapbox:layout_constraintTop_toTopOf="@+id/geocode_latitude_editText"
tools:layout_editor_absoluteX="116dp" />

<Button
android:id="@+id/start_geocode_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#37b426"
android:text="@string/geocode"
android:textAllCaps="false"
android:textColor="#FFFFFF"
mapbox:layout_constraintStart_toStartOf="@+id/geocode_latitude_editText"
mapbox:layout_constraintTop_toBottomOf="@+id/geocode_latitude_editText" />

<Button
android:id="@+id/map_center_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/mapboxRedDark"
android:text="@string/map_center"
android:textAllCaps="false"
android:textColor="#FFFFFF"
mapbox:layout_constraintStart_toStartOf="@+id/start_geocode_button"
mapbox:layout_constraintTop_toBottomOf="@+id/start_geocode_button" />

<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/geocoding_results"
android:textStyle="bold"
mapbox:layout_constraintStart_toEndOf="@+id/geocode_longitude_editText"
mapbox:layout_constraintTop_toTopOf="@+id/geocode_longitude_editText" />

<ScrollView
android:id="@+id/scrollView2"
android:layout_width="0dp"
android:layout_height="0dp"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toEndOf="@+id/geocode_longitude_editText"
mapbox:layout_constraintTop_toBottomOf="@+id/textView4">

<TextView
android:id="@+id/geocode_result_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="15sp"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toEndOf="@+id/geocode_latitude_editText"
mapbox:layout_constraintTop_toTopOf="@+id/geocode_latitude_editText" />

</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>


</androidx.cardview.widget.CardView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>
GeocodingActivity.java
package com.mapbox.mapboxandroiddemo.examples.javaservices;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ListPopupWindow;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mapbox.api.geocoding.v5.GeocodingCriteria;
import com.mapbox.api.geocoding.v5.MapboxGeocoding;
import com.mapbox.api.geocoding.v5.models.CarmenFeature;
import com.mapbox.api.geocoding.v5.models.GeocodingResponse;
import com.mapbox.core.exceptions.ServicesException;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

/**
* Use the Mapbox Geocoding API to retrieve various information about a set of coordinates.
*/
public class GeocodingActivity extends AppCompatActivity implements OnMapReadyCallback {

private MapView mapView;
private MapboxMap mapboxMap;
private Button chooseCityButton;
private EditText latEditText;
private EditText longEditText;
private TextView geocodeResultTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_javaservices_geocoding);

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
initTextViews();
initButtons();
}
});
}

private void initTextViews() {
latEditText = findViewById(R.id.geocode_latitude_editText);
longEditText = findViewById(R.id.geocode_longitude_editText);
geocodeResultTextView = findViewById(R.id.geocode_result_message);
}

private void initButtons() {
Button mapCenterButton = findViewById(R.id.map_center_button);
Button startGeocodeButton = findViewById(R.id.start_geocode_button);
chooseCityButton = findViewById(R.id.choose_city_spinner_button);
startGeocodeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Make sure the EditTexts aren't empty
if (TextUtils.isEmpty(latEditText.getText().toString())) {
latEditText.setError(getString(R.string.fill_in_a_value));
} else if (TextUtils.isEmpty(longEditText.getText().toString())) {
longEditText.setError(getString(R.string.fill_in_a_value));
} else {
if (latCoordinateIsValid(Double.valueOf(latEditText.getText().toString()))
&& longCoordinateIsValid(Double.valueOf(longEditText.getText().toString()))) {
// Make a geocoding search with the values inputted into the EditTexts
makeGeocodeSearch(new LatLng(Double.valueOf(latEditText.getText().toString()),
Double.valueOf(longEditText.getText().toString())));
} else {
Toast.makeText(GeocodingActivity.this, R.string.make_valid_lat, Toast.LENGTH_LONG).show();
}
}
}
});
chooseCityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCityListMenu();
}
});
mapCenterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get the map's target
LatLng target = mapboxMap.getCameraPosition().target;

// Fill the coordinate EditTexts with the target's coordinates
setCoordinateEditTexts(target);

// Make a geocoding search with the target's coordinates
makeGeocodeSearch(target);

}
});
}

private boolean latCoordinateIsValid(double value) {
return value >= -90 && value <= 90;
}

private boolean longCoordinateIsValid(double value) {
return value >= -180 && value <= 180;
}

private void setCoordinateEditTexts(LatLng latLng) {
latEditText.setText(String.valueOf(latLng.getLatitude()));
longEditText.setText(String.valueOf(latLng.getLongitude()));
}

private void showCityListMenu() {
List<String> modes = new ArrayList<>();
modes.add("Vancouver");
modes.add("Helsinki");
modes.add("Lima");
modes.add("Osaka");
ArrayAdapter<String> profileAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, modes);
final ListPopupWindow listPopup = new ListPopupWindow(this);
listPopup.setAdapter(profileAdapter);
listPopup.setAnchorView(chooseCityButton);
listPopup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long longg) {
LatLng cityLatLng = new LatLng();
if (position == 0) {
// Vancouver
cityLatLng = new LatLng(49.2827, -123.1207);
setCoordinateEditTexts(cityLatLng);
} else if (position == 1) {
// Helsinki
cityLatLng = new LatLng(60.1698, 24.938);
setCoordinateEditTexts(cityLatLng);
} else if (position == 2) {
// Lima
cityLatLng = new LatLng(-12.0463, -77.0427);
setCoordinateEditTexts(cityLatLng);
} else if (position == 3) {
// Osaka
cityLatLng = new LatLng(34.693, 135.5021);
setCoordinateEditTexts(cityLatLng);
}
animateCameraToNewPosition(cityLatLng);
makeGeocodeSearch(cityLatLng);
listPopup.dismiss();
}
});
listPopup.show();
}

private void makeGeocodeSearch(final LatLng latLng) {
try {
// Build a Mapbox geocoding request
MapboxGeocoding client = MapboxGeocoding.builder()
.accessToken(getString(R.string.access_token))
.query(Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))
.geocodingTypes(GeocodingCriteria.TYPE_PLACE)
.mode(GeocodingCriteria.MODE_PLACES)
.build();
client.enqueueCall(new Callback<GeocodingResponse>() {
@Override
public void onResponse(Call<GeocodingResponse> call,
Response<GeocodingResponse> response) {
if (response.body() != null) {
List<CarmenFeature> results = response.body().features();
if (results.size() > 0) {

// Get the first Feature from the successful geocoding response
CarmenFeature feature = results.get(0);
geocodeResultTextView.setText(feature.toString());
animateCameraToNewPosition(latLng);
} else {
Toast.makeText(GeocodingActivity.this, R.string.no_results,
Toast.LENGTH_SHORT).show();
}
}
}

@Override
public void onFailure(Call<GeocodingResponse> call, Throwable throwable) {
Timber.e("Geocoding Failure: " + throwable.getMessage());
}
});
} catch (ServicesException servicesException) {
Timber.e("Error geocoding: " + servicesException.toString());
servicesException.printStackTrace();
}
}

private void animateCameraToNewPosition(LatLng latLng) {
mapboxMap.animateCamera(CameraUpdateFactory
.newCameraPosition(new CameraPosition.Builder()
.target(latLng)
.zoom(13)
.build()), 1500);
}

// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Was this example helpful?