地图盒子中的全局位置搜索在安卓设备上无法正常工作。

huangapple 未分类评论48阅读模式
英文:

Global location search in mapbox doesn't working on android

问题

我在使用Mapbox时遇到了问题。搜索地点功能无法正常工作。我尝试了很多方法,但都没有效果。
当我点击搜索按钮时,什么都不会发生,而且当我在搜索栏中输入地点时,也没有任何提示。

XML文件:

  1. <com.mapbox.mapboxsdk.plugins.places.autocomplete.ui.SearchView
  2. android:layout_width="match_parent"
  3. android:background="@drawable/layout_form_text"
  4. android:elevation="3dp"
  5. android:gravity="bottom|left"
  6. android:padding="10dp"
  7. android:layout_marginLeft="16dp"
  8. android:layout_marginRight="16dp"
  9. android:layout_marginBottom="19dp"
  10. android:layout_height="wrap_content"/>
  11. <com.google.android.material.floatingactionbutton.FloatingActionButton
  12. android:id="@+id/fab_location_search"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="end|bottom"
  16. android:layout_margin="16dp"
  17. android:tint="@android:color/white"
  18. app:backgroundTint="@color/colorPrimary"
  19. app:srcCompat="@android:drawable/ic_search_category_default" />

PlacePluginActivity文件:

  1. public class PlacesPluginActivity extends AppCompatActivity implements OnMapReadyCallback {
  2. private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
  3. private MapView mapView;
  4. private MapboxMap mapboxMap;
  5. private CarmenFeature home;
  6. private CarmenFeature work;
  7. private String geojsonSourceLayerId = "geojsonSourceLayerId";
  8. private String symbolIconId = "symbolIconId";
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. // 在此配置Mapbox访问令牌。这需要在应用程序对象或包含地图视图的同一活动中调用。
  13. Mapbox.getInstance(this, Token);
  14. // 这包含在XML中的MapView,并且需要在配置访问令牌后调用。
  15. setContentView(R.layout.activity_layout_map_priere);
  16. mapView = findViewById(R.id.mapView);
  17. mapView.onCreate(savedInstanceState);
  18. mapView.getMapAsync(this);
  19. }
  20. private void initSearchFab() {
  21. findViewById(R.id.fab_location_search).setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View view) {
  24. Intent intent = new PlaceAutocomplete.IntentBuilder()
  25. .accessToken("pk.eyJ1IjoibWVoZGluIiwiYSI6ImNrYTRobzR4YTB1MnAzbG9nMGljejBpb2UifQ.nG3vtoddC1TCHW3Skpkttg")
  26. .placeOptions(PlaceOptions.builder().build())
  27. .build(PlacesPluginActivity.this);
  28. startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
  29. }
  30. });
  31. }
  32. }

我不知道为什么它不起作用,我找不到问题所在。

英文:

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

  1. &lt;com.mapbox.mapboxsdk.plugins.places.autocomplete.ui.SearchView
  2. android:layout_width=&quot;match_parent&quot;
  3. android:background=&quot;@drawable/layout_form_text&quot;
  4. android:elevation=&quot;3dp&quot;
  5. android:gravity=&quot;bottom|left&quot;
  6. android:padding=&quot;10dp&quot;
  7. android:layout_marginLeft=&quot;16dp&quot;
  8. android:layout_marginRight=&quot;16dp&quot;
  9. android:layout_marginBottom=&quot;19dp&quot;
  10. android:layout_height=&quot;wrap_content&quot;/&gt;
  11. &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
  12. android:id=&quot;@+id/fab_location_search&quot;
  13. android:layout_width=&quot;wrap_content&quot;
  14. android:layout_height=&quot;wrap_content&quot;
  15. android:layout_gravity=&quot;end|bottom&quot;
  16. android:layout_margin=&quot;16dp&quot;
  17. android:tint=&quot;@android:color/white&quot;
  18. app:backgroundTint=&quot;@color/colorPrimary&quot;
  19. app:srcCompat=&quot;@android:drawable/ic_search_category_default&quot; /&gt;

PlacePluginActivity file :

  1. public class PlacesPluginActivity extends AppCompatActivity implements OnMapReadyCallback {
  2. private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
  3. private MapView mapView;
  4. private MapboxMap mapboxMap;
  5. private CarmenFeature home;
  6. private CarmenFeature work;
  7. private String geojsonSourceLayerId = &quot;geojsonSourceLayerId&quot;;
  8. private String symbolIconId = &quot;symbolIconId&quot;;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. // Mapbox access token is configured here. This needs to be called either in your application
  13. // object or in the same activity which contains the mapview.
  14. Mapbox.getInstance(this, Token);
  15. // This contains the MapView in XML and needs to be called after the access token is configured.
  16. setContentView(R.layout.activity_layout_map_priere);
  17. mapView = findViewById(R.id.mapView);
  18. mapView.onCreate(savedInstanceState);
  19. mapView.getMapAsync(this);
  20. }
  21. private void initSearchFab() {
  22. findViewById(R.id.fab_location_search).setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View view) {
  25. Intent intent = new PlaceAutocomplete.IntentBuilder()
  26. .accessToken(&quot;pk.eyJ1IjoibWVoZGluIiwiYSI6ImNrYTRobzR4YTB1MnAzbG9nMGljejBpb2UifQ.nG3vtoddC1TCHW3Skpkttg&quot;)
  27. .placeOptions(PlaceOptions.builder().build())
  28. .build(PlacesPluginActivity.this);
  29. startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
  30. }
  31. });
  32. }
  33. }

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/layoutactivity_picker_launcher.xmlactivity_places_fragment.xmlactivity_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.

huangapple
  • 本文由 发表于 2020年5月19日 21:18:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/61892053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定