[英]android google map issues
我目前正在构建一个android应用。 我了解基本知识,并且做了很多应用程序,但是现在我的项目更大了。 我的应用程序包括许多带有NavigationDrawer的屏幕,这些屏幕迫使我在要显示在应用程序上的每个类上使用Fragment类的扩展。 这是我的问题-不久前,我创建了一个简单的一页Google Maps屏幕的应用程序,它工作得很好。 我想在当前正在使用的应用程序上做同样的事情,但是当我转到页面时,它给我的就是左下角屏幕上的google日志,其余为灰色,如视图(未显示地图)。在包括本网站在内的很多地方搜索了解决方案,我得到的答案是,这可能与google提供给我的API密钥有关。我对此深信不疑,我敢肯定它不是来自密钥,因为我将其应用到了我的一页Google地图应用程序上,并且运行完美。我还检查了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"));
}
}
}
我需要该Fragment扩展才能使NavigationDrawer正常工作,所以我创建了一个内部类并在其中应用了地图的代码。
这是清单中的内容:
<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的一些概念。您的空闲时间!
我怀疑缺少某些部分:
(1)声明使用MAPS_RECEIVE
权限:
<permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<!-- after declare use this -->
<uses-permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE"/>
(2)缺少声明的gms版本变量:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
(3)API_KEY的生成可能存在一些问题:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key" />
请首先与上述3位嫌疑犯进行检查:如果仍然存在错误,则可以从此处重新确认启用Google地图的步骤
仔细检查与API_KEY相关的内容
检查APK签名。 您必须在测试中分配调试API密钥,并在释放APK时分配Release API密钥。 插入正确的API密钥,清理并重建项目。
我去过那里一年了,我可以保证这是问题所在。 仔细检查您的密钥,以及它是否是调试或发布密钥。
要检查的另一件事是您的应用程序的软件包名称...让我知道您是否已这样做,并且仍然可以看到灰色视图,而不是
更新:
当我意识到您的问题仅在签名阶段时,我试图包含所有内容。
您只需要获取调试密钥库的SHA-1指纹并将其放在“ API密钥”对话框中即可。 并相信我,没有什么我可以写的比Google文档更清楚,更有意义的了。
请按照此链接中的步骤获取调试证书和SHA-1指纹 。
更新2:
为了获得密钥库的SHA-1,您可以检查此线程 。
您可以使用SupportMapFragment在片段中显示地图。
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">
<fragment
android:id="@+id/mapView"
android:layout_marginTop="-470dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
和你的班级如下
public class guide extends Fragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.test, container, false);
((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.mapView)).getMapAsync (new OnMapReadyCallback(){
@Override
public void onMapReady(final GoogleMap map) {
this.mMap = map;
mMap.setMyLocationEnabled(true);
setUpMap();
}
});
return v;
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
注意:如果找不到SupportMapFragment的getMapAsync方法,请更新您的Google Play服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.