【发布时间】:2015-04-15 13:30:43
【问题描述】:
我目前正在构建一个安卓应用程序。我了解基础知识,我做了很多应用程序,但现在我的项目更大了。我的应用程序包含许多包含 NavigationDrawer 的屏幕,这迫使我在我想在我的应用程序上显示的每个类上使用 Fragment 类的扩展。 这是我的问题-前段时间我创建了一个简单的单页谷歌地图屏幕应用程序,它工作得很好。我想在我目前正在开发的应用程序上做同样的事情,但是当我转到页面时它给我的只是左下角屏幕上的谷歌日志,其余的都是灰色的视图(没有显示地图)。我在包括这个网站在内的很多地方搜索了解决方案,我可以得到的答案是它可能与我的 google 给我的 API 密钥有关。我确认了这一点,我很确定它不是来自密钥,因为我将它应用到我的一页谷歌地图应用程序上,它运行良好。我还拥有我检查了 6 次所需的所有权限。所以我的结论是代码中有一些东西阻止了地图的显示。
这是我的代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class guide extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.test, container,
false);
return v;
}
class HoldFragmentPlace extends FragmentActivity{
private GoogleMap mMap;
public HoldFragmentPlace(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.test, container,
false);
return v;
}
@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();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
}
我需要让我的 NavigationDrawer 工作的 Fragment 扩展,因此我创建了一个内部类并在那里应用了地图的代码。
这是我的清单中的内容:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
这是我的地图 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="-470dp" />
</LinearLayout>
我还尝试了 Java 代码的其他一些变体,其中一些不包含内部类,结果仍然相同。我要么遗漏了一些非常基本的东西,要么误解了有关 Android 的一些概念。如果您已阅读感谢您抽出宝贵的时间!
【问题讨论】:
标签: java android google-maps