英文:
Global location search in mapbox doesn't working on android
问题
我在使用Mapbox时遇到了问题。搜索地点功能无法正常工作。我尝试了很多方法,但都没有效果。
当我点击搜索按钮时,什么都不会发生,而且当我在搜索栏中输入地点时,也没有任何提示。
XML文件:
<com.mapbox.mapboxsdk.plugins.places.autocomplete.ui.SearchView
android:layout_width="match_parent"
android:background="@drawable/layout_form_text"
android:elevation="3dp"
android:gravity="bottom|left"
android:padding="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="19dp"
android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_location_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:tint="@android:color/white"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@android:drawable/ic_search_category_default" />
PlacePluginActivity文件:
public class PlacesPluginActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
private MapView mapView;
private MapboxMap mapboxMap;
private CarmenFeature home;
private CarmenFeature work;
private String geojsonSourceLayerId = "geojsonSourceLayerId";
private String symbolIconId = "symbolIconId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在此配置Mapbox访问令牌。这需要在应用程序对象或包含地图视图的同一活动中调用。
Mapbox.getInstance(this, Token);
// 这包含在XML中的MapView,并且需要在配置访问令牌后调用。
setContentView(R.layout.activity_layout_map_priere);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
private void initSearchFab() {
findViewById(R.id.fab_location_search).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new PlaceAutocomplete.IntentBuilder()
.accessToken("pk.eyJ1IjoibWVoZGluIiwiYSI6ImNrYTRobzR4YTB1MnAzbG9nMGljejBpb2UifQ.nG3vtoddC1TCHW3Skpkttg")
.placeOptions(PlaceOptions.builder().build())
.build(PlacesPluginActivity.this);
startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
}
});
}
}
我不知道为什么它不起作用,我找不到问题所在。
英文:
I'm stuck on a problem with mapbox. <br>
The search place doesn't work. <br>
I have tried many things but nothing works.
When i click on the search button, nothing happen, and when i'm searching a place(by tipping in the search bar), nothing happen too, there is no suggestion
The xml:file
<com.mapbox.mapboxsdk.plugins.places.autocomplete.ui.SearchView
android:layout_width="match_parent"
android:background="@drawable/layout_form_text"
android:elevation="3dp"
android:gravity="bottom|left"
android:padding="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="19dp"
android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_location_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:tint="@android:color/white"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@android:drawable/ic_search_category_default" />
PlacePluginActivity file :
public class PlacesPluginActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
private MapView mapView;
private MapboxMap mapboxMap;
private CarmenFeature home;
private CarmenFeature work;
private String geojsonSourceLayerId = "geojsonSourceLayerId";
private String symbolIconId = "symbolIconId";
@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, Token);
// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_layout_map_priere);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
private void initSearchFab() {
findViewById(R.id.fab_location_search).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new PlaceAutocomplete.IntentBuilder()
.accessToken("pk.eyJ1IjoibWVoZGluIiwiYSI6ImNrYTRobzR4YTB1MnAzbG9nMGljejBpb2UifQ.nG3vtoddC1TCHW3Skpkttg")
.placeOptions(PlaceOptions.builder().build())
.build(PlacesPluginActivity.this);
startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
}
});
}
}
I don't know why it's not working, I can't find the problem here
答案1
得分: 1
使用在XML中直接使用SearchView
可能是问题所在。SearchView
在插件内部被使用。
https://github.com/mapbox/mapbox-plugins-android/tree/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/places 展示了设置插件的各种选项。确保查看每个示例活动的XML布局文件 https://github.com/mapbox/mapbox-plugins-android/tree/83b191404b2d53da30f14fd7c671fe0b3eb9c4b6/app/src/main/res/layout (activity_picker_launcher.xml
、activity_places_fragment.xml
和activity_places_launcher.xml
)
我会克隆仓库,在 https://github.com/mapbox/mapbox-plugins-android/blob/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/PluginApplication.kt#L20 添加你的令牌,然后在你的设备上运行该应用,以便你可以看到示例的外观。
另外,在公共网站上小心发布你的Mapbox令牌。我会在 https://account.mapbox.com/access-tokens/ 旋转令牌,这样其他人就不能使用你当前暴露的令牌。
英文:
Using the SearchView
directly in your XML is probably the problem. The SearchView
is used internally by the plugin.
https://github.com/mapbox/mapbox-plugins-android/tree/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/places shows your various options for setting up the plugin. Make sure to look at the XML layout file for each example activity https://github.com/mapbox/mapbox-plugins-android/tree/83b191404b2d53da30f14fd7c671fe0b3eb9c4b6/app/src/main/res/layout (activity_picker_launcher.xml
, activity_places_fragment.xml, and
activity_places_launcher.xml`)
I'd clone the repo, add your token at https://github.com/mapbox/mapbox-plugins-android/blob/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/PluginApplication.kt#L20, and then run the app on your device so that you can see what the examples look like.
Also, be careful about posting your Mapbox token on a public site. I'd rotate the token at https://account.mapbox.com/access-tokens/ so that others can't use your currently exposed one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论