我使用默认代码显示地图,但我想知道为什么它不起作用。我已按照“入门”中显示的步骤进行操作Google Maps API。我为应用程序生成了一个API密钥,并且还限制了它。
这是 MapsActivity.java
的代码public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
double LATITUDE, LONGITUDE;
String MARKER;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Bundle lalo = getIntent().getExtras();
if (lalo != null){
LATITUDE = Double.valueOf(lalo.getString("LAT"));
LONGITUDE = Double.valueOf(lalo.getString("LON"));
MARKER = lalo.getString("MARKER");
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng location = new LatLng(LATITUDE, LONGITUDE);
mMap.addMarker(new MarkerOptions().position(location).title(MARKER));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}
}
以下是MapActivity的截图。
请帮我解决这个问题。
答案 0 :(得分:0)
您是否使用版本密钥用于名称包含“.debug”的调试版本?这也会引发这种情况。您可以显示日志以获取更多详细信息。
答案 1 :(得分:0)
试试这个。它对我有用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.onCreate(savedInstanceState);
mapFragment.onResume();
mapFragment.getMapAsync(this);
}
@Override
public void onResume() {
mapFragment.onResume();
super.onResume();
}
@Override
public void onPause() {
mapFragment.onPause();
super.onPause();
}
@Override
public void onDestroy() {
mapFragment.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
mapFragment.onLowMemory();
super.onLowMemory();
}
答案 2 :(得分:0)
我解决了这个问题,因为IntelliJ Amiya告诉他寻找logcat。 logcat说授权失败然后我解决了去google maps api控制台。在那里,我看到我的API是正确的,但SHA1键是不正确的,所以我从终端生成一个并保存。我没有找到启用Google Maps Android API v2的任何选项。但我并不需要启用Google Maps Android API v2。另外,我刚刚放置了正确的和新生成的SHA1密钥并对其进行了纠正。
感谢大家的宝贵建议。