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

huangapple go评论87阅读模式
英文:

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

&lt;com.mapbox.mapboxsdk.plugins.places.autocomplete.ui.SearchView
    android:layout_width=&quot;match_parent&quot;
    android:background=&quot;@drawable/layout_form_text&quot;
    android:elevation=&quot;3dp&quot;
    android:gravity=&quot;bottom|left&quot;
    android:padding=&quot;10dp&quot;
    android:layout_marginLeft=&quot;16dp&quot;
    android:layout_marginRight=&quot;16dp&quot;
    android:layout_marginBottom=&quot;19dp&quot;
    android:layout_height=&quot;wrap_content&quot;/&gt;

&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id=&quot;@+id/fab_location_search&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_gravity=&quot;end|bottom&quot;
    android:layout_margin=&quot;16dp&quot;
    android:tint=&quot;@android:color/white&quot;
    app:backgroundTint=&quot;@color/colorPrimary&quot;
    app:srcCompat=&quot;@android:drawable/ic_search_category_default&quot; /&gt;

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 = &quot;geojsonSourceLayerId&quot;;
	private String symbolIconId = &quot;symbolIconId&quot;;

	@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(&quot;pk.eyJ1IjoibWVoZGluIiwiYSI6ImNrYTRobzR4YTB1MnAzbG9nMGljejBpb2UifQ.nG3vtoddC1TCHW3Skpkttg&quot;)
						.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/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://go.coder-hub.com/61892053.html
匿名

发表评论

匿名网友

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

确定