我正在尝试运行一个简单的谷歌 Map ,但它没有工作它只显示 Map 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)));
}
}
}
3 回答
在您的清单文件中,您必须添加:
喜欢从: your.application.package 到: com.themontcalm.droid
是的,如果你正在使用
然后从清单文件中删除:
并将您的api代码添加到清单文件中:
在清单文件中,你错过了这个:
所以也在清单文件中添加:
您应该在代码中使用 R.id.map 更改 android.R.id.content
就像是:
为了使用谷歌 Map 服务,
请访问code.google.com并获取ApiKey .
在那里创建一个项目 - >转到服务 - >激活Google Maps Android API v2 . 然后为您的应用程序创建密钥 .
将以下权限添加到清单中 .
使用
还添加
在清单中的应用程序标记内 .
使用
在你的代码中 .
并设置其他所需的属性 . 现在运行应用程序 .
在你的清单中添加
还要添加到您的应用程序标签
您还可以访问Google Map V2,this和Example of map v2
希望它能帮到你 .
Edit:
按照你的说法在你的项目中添加这个xml文件
并在 Map 活动中访问它