问题是我向我的应用程序添加了GoogleMap
个活动,我甚至得到了密钥,但地图只显示在模拟器中。当我在不同的设备上下载我的应用程序时,地图无法正常工作。
所有我能看到的只是一个空洞的观点,但是" Google"字符串和" + - "键。我认为加载时间太长但我觉得用wi-fi不能超过10分钟。这是我的代码:
MapsActivity.java
public class MapsActivity extends ActionBarActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private static final LatLng TRACKTOR = new LatLng(55.1747, 61.2871);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if enabled and if not send user to the GPS settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
// Move the camera instantly to Sydney with a zoom of 15.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(TRACKTOR, 15));
// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(TRACKTOR) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
Log.w("MAP","is showing now!");
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
} else Log.w("MAP","is not showing!");
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(55.1747, 61.2871)).title("Ледовая арена \"Трактор\""));
mMap.setBuildingsEnabled(true);
}
}
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.project1.2014.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
答案 0 :(得分:2)
答案 1 :(得分:0)
获取Google Maps API Key
并添加到AndroidManifest.xml
<application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDSWlBiYKjYujZeJ_312345g0qXpxuaKZY" />
</application>
<强> Obtain Google Maps API Key 强>
答案 2 :(得分:0)
您可能通过使用调试密钥和使用调试映射密钥对应用程序进行签名来安装应用程序,或者至少是版本1 Maps API的问题。不确定版本2.因此,请尝试签名,就好像您要在Play商店发布它,然后将其安装在手机上,看看它是否有效。
对于版本1 API,您必须在包含Maps对象的布局文件中放入正确的(生产与调试)密钥。