Как указано в названии, моя страна, к сожалению, еще не охвачена геодезистом Mapbox, поэтому до тех пор, пока мне не придется делать это с андроидом. Проблема в том, что я изо всех сил пытаюсь заставить геокодер android работать вместо Mapbox.Как использовать геокодер android с Mapbox вместо Geocoder Mapbox?
Я пытаюсь получить пример камеры Mapbox, чтобы переместить камеру по адресу, который я ввел в поле editText.
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);
}
Вот мой .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" />
Эй вы могли бы уточнить, используете ли вы геокодер Google Android или геокодер [MAS geocoder] (https://www.mapbox.com/android-sdk/#geocoder)? – cammace
Я использую стандартный Google Geocoder Google, к сожалению Геокодер Mapbox не работает в Южной Африке. – Brian
Я все еще не смог понять Geocoder, есть ли у вас какие-либо предложения относительно того, как я мог бы подойти к нему? :) – Brian