Brian
Brian

Reputation: 107

How to use the android Geocoder with Mapbox instead of the Mapbox Geocoder?

AS the title states, my country unfortunately is not yet covered by the Mapbox Geocoder, so until it is I have to make due with the android one. The problem is that I am struggling to get the android geocoder to work in place of the Mapbox one.

I'm trying to get the Camera Mapbox example to move the camera to the address I put into the editText box.

mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final MapboxMap mapboxMap) {

            // When user clicks the map, animate to new camera location
            EditText etAddress1 = (EditText) findViewById(R.id.etAddress);
            String location = etAddress1.getText().toString();

            Geocoder gc = new Geocoder(this);
            List<Address> list = gc.getFromLocationName(location, 1);

            if (list.size() > 0) {

                Address add = list.get(0);
                String locality = add.getLocality();
                Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

                end = Position.fromCoordinates(add.getLongitude(), add.getLatitude());

                GoBTN.setOnClickListener(new View.OnClickListener(){
                @Override
                public void OnClick(View v) {
                    CameraPosition position = new CameraPosition.Builder()
                            .target(end.getLatitude(), end.getLongitude()) // Sets the new camera position
                            .zoom(17) // Sets the zoom
                            .bearing(180) // Rotate the camera
                            .tilt(30) // Set the camera tilt
                            .build(); // Creates a CameraPosition from the builder

                    mapboxMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(position), 7000);
                }
            });
        }
    });
}

public void geoLocate (View v) throws IOException {
    hideSoftKeyboard(v);

    EditText etAddress1 = (EditText) findViewById(R.id.etAddress);
    String location = etAddress1.getText().toString();

    Geocoder gc = new Geocoder(this);
    List<Address> list = gc.getFromLocationName(location, 2);

    if (list.size() > 0) {

        Address add = list.get(0);
        String locality = add.getLocality();
        Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

        double lat = add.getLatitude();
        double lng = add.getLongitude();

        CameraPosition position = new CameraPosition.Builder()
                .target(new LatLng(lat, lng)) // Sets the new camera position
                .zoom(17) // Sets the zoom
                .bearing(180) // Rotate the camera
                .tilt(30) // Set the camera tilt
                .build(); // Creates a CameraPosition from the builder

        mapboxMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(position), 7000);

    }
}

private void hideSoftKeyboard(View v) {
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

Here is my .xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- Set the starting camera position and map style using xml-->
<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:style_url="mapbox://styles/mapbox/streets-v9"
    mapbox:center_latitude="40.73581"
    mapbox:center_longitude="-73.99155"
    mapbox:zoom="11"
    mapbox:access_token="my access token"/>

<EditText
    android:id="@+id/etAddress"
    android:singleLine="true"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="18dp"
    android:layout_marginEnd="18dp"
    android:layout_marginTop="14dp"
    android:padding="10dp"
    android:hint="Search Location"
    android:textColor="@android:color/black"
    android:background="@android:color/white"
    android:elevation="12dp"/>

<Button
    android:id="@+id/btnGo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|center_vertical"
    android:text="Go"
    android:onClick="geoLocate"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/mapView"
    android:layout_alignEnd="@+id/mapView" />

Upvotes: 0

Views: 860

Answers (1)

RobLabs
RobLabs

Reputation: 2347

It looks like the Mapbox Geocoder works for South Africa. I was successful in Geocoding this address. Here is the result, using the Mapbox Command Line Interface.

mapbox geocoding 'Pretoria'

This is a great tool to verify if the Mapbox services do what you need it to do.


Now, back to your Android problem. If the Mapbox Geocoding works for your particular place in South Africa, then you can see this example on how to use Geocoding using Mapbox Android Services. (also posted by @cammace)

Upvotes: 0

Related Questions