【发布时间】:2016-07-08 10:27:39
【问题描述】:
正如标题所述,不幸的是我的国家还没有被 Mapbox 地理编码器覆盖,所以在它之前我必须使用 android 的。问题是我正在努力让 android 地理编码器代替 Mapbox 工作。
我正在尝试让 Camera 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?
-
我使用的是标准的 Google Android 地理编码器,不幸的是 Mapbox 地理编码器在南非无法使用。
-
我仍然无法弄清楚地理编码器,您对我如何处理它有任何建议吗? :)
标签: android geolocation geocoding mapbox