简体   繁体   English

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

[英]Opening Google Directions API internally Android Studio

I am currently developing an application in Android Studio and I have included both the Google Maps API and Google Directions API within the app. 我目前正在Android Studio中开发应用程序,并且在该应用程序中同时包含了Google Maps API和Google Directions API。 I have included a button in my app which allows the user to be guided to their selected location. 我在我的应用程序中添加了一个按钮,该按钮可将用户引导到他们选择的位置。

The following code uses the Google Maps API, opens up Google Maps within the application but shows the marker on the maps without giving directions to that place: 以下代码使用Google Maps API,在应用程序中打开Goog​​le 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));

The below code uses the Google Directions API and gives the user a guide to their selected destination: 以下代码使用了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);

The only problem being the above code relating to the Google Directions API, opens the Google Maps application outside my application. 唯一的问题是上面与Google Directions API有关的代码,在我的应用程序外部打开了Google Maps应用程序。 I was wondering how to get it to open within the application itself. 我想知道如何在应用程序本身中打开它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM