谷歌地图不适用于模拟器和设备

问题描述 投票:1回答:3

我正在尝试运行一个简单的谷歌地图,但它没有工作它只显示地图background.checked模拟器和设备。

xml文件: -

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

Java类

import android.os.Bundle; import
com.google.android.gms.maps.GoogleMap; import
com.google.android.gms.maps.SupportMapFragment; import
com.google.android.gms.maps.model.BitmapDescriptorFactory; import
com.google.android.gms.maps.model.LatLng; import
com.google.android.gms.maps.model.Marker; import
com.google.android.gms.maps.model.MarkerOptions;

public class LocationGB extends android.support.v4.app.FragmentActivity {     
    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;
     @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();

        if (map!=null){
            Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher))); 
        }
    } 
}
android android-mapview android-maps
3个回答
2
投票

在您的清单文件中,您必须添加:

<permission
  android:name="your.application.package.permission.MAPS_RECEIVE"
  android:protectionLevel="signature" />

喜欢从:your.application.package到:com.themontcalm.droid

是的,如果你正在使用

<uses-library android:name="com.google.android.maps" />

然后从清单文件中删除:

并将您的api代码添加到清单文件中:

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your_Map_Key" />

在清单文件中,你错过了这个:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

所以也在清单文件中添加:

你应该在你的代码中用R.id.map更改android.R.id.content

就像是:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

2
投票

为了使用谷歌地图服务,

请访问code.google.com并获取ApiKey。

在那里创建一个项目 - >转到服务 - >激活Google Maps Android API v2。然后为您的应用程序创建密钥。

将以下权限添加到清单中。

 <uses-permission android:name="com.anchit.locationapi.maps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" />

使用

 <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your_apikey" />

还添加

<permission
        android:name="com.anchit.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

在清单中的应用程序标记内。

使用

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

在你的代码中。

并设置其他所需的属性。现在运行应用程序。


1
投票

在你的清单中添加

<permission
  android:name="your.application.package.permission.MAPS_RECEIVE"
  android:protectionLevel="signature" />

还要添加到您的应用程序标签

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Your_Map_Key" />

您还可以参观Google Map V2thisExample of map v2

希望它能帮到你。

编辑:

按照你的说法在你的项目中添加这个xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

并在地图活动中访问它

 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap(); 
© www.soinside.com 2019 - 2024. All rights reserved.