我一直在努力解决这个问题,但我在网上发现的一切都没有用。我已经为我的项目获得了一个不受限制的密钥,但地图仍然无法正确显示。返回的状态被视为正常,但不显示任何内容。我还确保为该密钥启用两个API。下面是一些图片,显示了我的代码后面的样子。如果我需要包含任何其他代码,只需评论您需要的内容。我还使用了https://github.com/akexorcist/Android-GoogleDirectionLibrary
中的Google方向库单击按钮后,它会转到正确的区域,但一切都是灰色的,所以我相信我的谷歌地图有问题。
依赖关系
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'
}
XML Fragment
<?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="<did not show my package>.Directions">
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="<did not show my package>.MapsActivity" />
<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>
Directions.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
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 = "<server key not shown>";
@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));
}
}
清单元数据
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>
清单权限
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
答案 0 :(得分:0)
无论如何,您可以尝试申请custom style to Google Map。对于地图样式JSON准备,您可以使用Styling Wizard并将带有样式描述的JSON文件(例如map_style.json
)保存到res/raw
文件夹中。而不是在onMapReady()
回调中应用自定义样式:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mGoogleMap;
private MapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = mGoogleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.map_style));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
}
}