【发布时间】:2016-05-04 16:11:52
【问题描述】:
我目前正在 Android Studio 中开发一个应用程序,并且我在应用程序中包含了 Google Maps API 和 Google Directions API。我在我的应用程序中包含了一个按钮,它允许用户被引导到他们选择的位置。
以下代码使用 Google Maps API,在应用程序中打开 Google Maps,但在地图上显示标记,但不提供前往该地点的方向:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker at the Oval and move the camera
LatLng oval = new LatLng(53.3484013,-6.2605243);
mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));
以下代码使用 Google Directions API 并为用户提供前往所选目的地的指南:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Uri gmmIntentUri = Uri.parse("google.navigation:q=The+Oval+Bar,+Dublin+Ireland");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
唯一的问题是上述与 Google Directions API 相关的代码会在我的应用程序之外打开 Google Maps 应用程序。我想知道如何让它在应用程序本身中打开。
【问题讨论】:
-
您需要解析从 Directions Api 接收到的数据并将路径绘制到 MapView 上。
标签: android api google-maps android-studio google-maps-api-3