在Android Studio内部打开Goog​​le Directions API

时间:2016-01-27 15:50:02

标签: android api google-maps android-studio google-maps-api-3

我目前正在Android Studio中开发应用程序,我在应用程序中包含了Google Maps API和Google Directions API。我在我的应用程序中添加了一个按钮,允许用户被引导到他们选择的位置。

以下代码使用Google Maps API,在应用程序中打开Goog​​le地图,但在地图上显示标记,但未向该地点发出指示:

 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相关的上述代码,会在我的应用程序之外打开Goog​​le Maps应用程序。我想知道如何在应用程序本身内打开它。

1 个答案:

答案 0 :(得分:0)

你应该使用片段com.google.android.gms.maps.MapFragment,如这篇伟大的Lars Vogel文章所示:http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html