1

我正在尝试运行一个简单的谷歌地图,但它不工作它只显示地图背景。在模拟器和设备上检查。

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))); 
        }
    } 
}
4

3 回答 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();
于 2013-08-29T05:03:11.270 回答
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();

在你的代码中。

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

于 2013-08-29T05:21:18.057 回答
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 V2地图 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(); 
于 2013-08-29T05:13:27.203 回答