在没有操作栏的应用程序中使用Mapbox PlacePicker activity,可以通过以下步骤实现:
以下是一个示例代码,展示了如何在没有操作栏的应用程序中使用Mapbox PlacePicker activity:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.places.picker.PlacePicker;
import com.mapbox.mapboxsdk.plugins.places.picker.model.PlacePickerOptions;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
private MapboxMap mapboxMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, "YOUR_MAPBOX_ACCESS_TOKEN");
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
findViewById(R.id.btnOpenPlacePicker).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Intent intent;
try {
intent = builder.build(MainActivity.this);
startActivityForResult(intent, PlacePicker.PLACE_PICKER_REQUEST);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PlacePicker.PLACE_PICKER_REQUEST && resultCode == RESULT_OK) {
com.mapbox.mapboxsdk.plugins.places.picker.model.Place place = PlacePicker.getPlace(data);
// 处理用户选择的地点
}
}
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
请注意,上述代码中的"YOUR_MAPBOX_ACCESS_TOKEN"应替换为你自己的Mapbox访问令牌。
这样,当用户点击"btnOpenPlacePicker"按钮时,将会启动Mapbox PlacePicker activity,并在用户选择地点后返回选择的结果。你可以在onActivityResult()方法中处理这些结果。
领取专属 10元无门槛券
手把手带您无忧上云