Google地图未显示在我的应用中

时间:2017-11-18 22:46:20

标签: android google-maps google-direction

点击获取路线按钮后,我收到以下地图。

Google Map with no detail

Map before Click

Map After Click

我已经确认原点和目的地是正确的,但我无法弄清楚为什么没有出现。

我正在使用https://github.com/akexorcist/Android-GoogleDirectionLibrary

中的Google方向库

以下是我的代码和所有相关文件:

Gradle依赖

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.akexorcist:googledirectionlibrary:1.0.5'
    testCompile 'junit:junit:4.12'
}

Directions.java

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.akexorcist.googledirection.DirectionCallback;
import com.akexorcist.googledirection.GoogleDirection;
import com.akexorcist.googledirection.constant.TransportMode;
import com.akexorcist.googledirection.model.Direction;
import com.akexorcist.googledirection.model.Route;
import com.akexorcist.googledirection.util.DirectionConverter;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;

public class Directions extends AppCompatActivity implements OnMapReadyCallback,
                                View.OnClickListener, DirectionCallback
{
    private GoogleMap googleMap;
    private LatLng origin = new LatLng(29.572813, -97.984900);
    private LatLng destination;
    private Button get_directions_btn;
    private String serverKey = <serverkey>;

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


        // origin = new LatLng(29.572813, -97.984900);

        Bundle bundle = getIntent().getExtras();
        double destLatitude = bundle.getDouble("latitude");
        double destLongitude = bundle.getDouble("longitude");
        Log.d("Latitude", "Latitude is: " + destLatitude);
        Log.d("Longitude", "Longitude is:" + destLongitude);
        destination = new LatLng(destLatitude, destLongitude);

        get_directions_btn = (Button) findViewById(R.id.get_directions_btn);
        get_directions_btn.setOnClickListener(this);

        ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment)).getMapAsync(this);
    }

    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
    }

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.get_directions_btn) {
            requestDirection();

        }
    }

    public void requestDirection() {
        Log.d("RequestOrigin", "Origin inside request: " + origin);
        Log.d("RequestDestination", "Destination inside request: " + destination);
        Snackbar.make(get_directions_btn, "Direction Requesting...", Snackbar.LENGTH_SHORT).show();
        GoogleDirection.withServerKey(serverKey)
                .from(origin)
                .to(destination)
                .transportMode(TransportMode.DRIVING)
                .execute(this);
    }

    public void onDirectionSuccess(Direction direction, String rawBody) {
        Log.d("Status","Direction Status is:" +direction.getStatus());
        Snackbar.make(get_directions_btn, "Success with status : " + direction.getStatus(), Snackbar.LENGTH_SHORT).show();
        if (direction.isOK()) {
            Route route = direction.getRouteList().get(0);
            Log.d("Route ", "Route is: " + route);

            Log.d("Origin Marker", "Origin Marker: " + origin);
            Log.d("Destination Marker", "Destination Marker: " + destination);
            googleMap.addMarker(new MarkerOptions().position(origin));
            googleMap.addMarker(new MarkerOptions().position(destination));

            ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
            googleMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.RED));
            setCameraWithCoordinationBounds(route);

            get_directions_btn.setVisibility(View.GONE);

        }
        else{
            Log.d("Status","Direction Status is:" +direction.getStatus());
            Snackbar.make(get_directions_btn, direction.getStatus(), Snackbar.LENGTH_SHORT).show();
        }
    }

        public void onDirectionFailure(Throwable t) {
            Snackbar.make(get_directions_btn, t.getMessage(), Snackbar.LENGTH_SHORT).show();
        }


    private void setCameraWithCoordinationBounds(Route route) {
        LatLng southwest = route.getBound().getSouthwestCoordination().getCoordination();
        LatLng northeast = route.getBound().getNortheastCoordination().getCoordination();
        LatLngBounds bounds = new LatLngBounds(southwest, northeast);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
    }
}

XML布局:activity_directions

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_directions"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Directions">

    <fragment
        android:id="@+id/map_fragment"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapActivity" />

    <Button
        android:id="@+id/get_directions_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Get Directions" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

确保您已在http://nlu.rasa.ai/python.html

中启用 Google Maps Android API Google Maps Direction API

https://console.developers.google.com

您的API密钥将位于凭据标签上。

还要确保清单中有一个元数据标记,如下所示:

 <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_KEY" />

并且为你提供了一些文档if not yet u can try to enabled api

编辑:

也许恰好是您的模拟器没有使用Google API目标,尝试安装另一个支持Google API的模拟器

这里有一些图片 https://developers.google.com/maps/documentation/android-api/start