2013-08-29 79 views
0

我想运行一个简单的谷歌地图,但它不工作 只在地图上显示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"/> 

进口android.os.Bundle;导入 com.google.android.gms.maps.GoogleMap;导入 com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory;导入 com.google.android.gms.maps.model.LatLng;导入 com.google.android.gms.maps.model.Marker;导入 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);静态最终LatLng KIEL =新 LatLng(53.551,9.993);私人GoogleMap地图; @Override保护无效的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))); 
    } 
     } } 
+0

只是一个通常的检查...你在清单中添加了互联网许可吗? – amalBit

+0

是的其他网络服务工作正常 –

+0

看看这[问题](http://stackoverflow.com/q/2199403/2219600)..它可能是一个错误... – amalBit

回答

1

在清单文件必须添加:

<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" /> 

而且在manifest文件你缺少这样的:

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

所以也增加了这也在清单文件中:

你应该改变android.R.id.contentR.id.地图在你的代码

喜欢的东西:

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

我把它放在清单文件中,但它抛出相同的错误 –

+0

它不工作的工作仍然抛出相同的错误 –

+1

好吧...现在发布您的Java类文件 – Piyush

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

希望它可以帮助你。

编辑:

按你说在你的项目

<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(); 
+0

其不工作的工作仍然会抛出相同的错误 –

+0

如果您正在尝试v2以上,则三条链接足以在您的应用中创建地图。 @VijayLaxmi – Andrain

+0

现在我正在使用v2,现在它的抛出java.lang.NullPointerException在: - map =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(android.R.id.content))。getMap(); –

2

添加此XML文件为了使用谷歌地图服务,

请访问code.google.com 并让你的ApiKey。

在那里创建一个项目 - >去服务 - >激活谷歌地图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(); 

在你的代码。

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

相关问题