gpt4 book ai didi

android - 我如何调整 Google map 工具栏以触发 "generic" Intent - Android

转载 作者:行者123 更新时间:2023-11-29 01:14:45 24 4
gpt4 key购买 nike

我在我的应用中实现了 Google map ,以显示带有导航按钮的工具栏。

文档: https://developers.google.com/maps/documentation/android-api/controls#map_toolbar

Toolbar

但它会触发特定的 Google map Intent 。

我能否调整它以触发“通用” Intent ,以便用户可以选择他们选择的导航应用程序?

提前感谢您的宝贵时间。

最佳答案

您无法控制 map 工具栏。根据the documentation (强调我的):

The toolbar displays icons that provide access to a map view or directions request in the Google Maps mobile app

如果您想启动自己的 Intent,您需要通过 googleMap.getUiSettings().setMapToolbarEnabled(false); 禁用此工具栏,然后实现您自己的 GoogleMap.OnMarkerClickListener 以显示您的自定义按钮。


解决方法(但很丑陋)可能是找到 Google map 方向按钮(带有 GoogleMapDirectionsButton 标记)并覆盖它的功能。这是一个工作示例:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private SupportMapFragment mapFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMapToolbarEnabled(true);

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40.42, -3.7), 17));
googleMap.addMarker(new MarkerOptions().position(new LatLng(40.42, -3.7)));

googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
View v = findGoogleMapDirectionsButton(mapFragment.getView());
if (v != null) {
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final LatLng targetLatLng = new LatLng(
marker.getPosition().latitude,
marker.getPosition().longitude);
final Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("google.navigation:q="
+ targetLatLng.latitude
+ ","
+ targetLatLng.longitude));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MapsActivity.this.startActivity(intent);
}
});
}

return false;
}
});
}

private View findGoogleMapDirectionsButton(View v) {
View directionsButton = null;

if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
directionsButton = findGoogleMapDirectionsButton(vg.getChildAt(i));
if (directionsButton != null) {
break;
}
}
} else if (v.getTag() != null
&& "GoogleMapDirectionsButton".equalsIgnoreCase(v.getTag().toString())) {
directionsButton = v;
}

return directionsButton;
}
}

关于android - 我如何调整 Google map 工具栏以触发 "generic" Intent - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40583421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com