Android 谷歌地图API v2不工作
我正在尝试使用GoogleMapsAPIv2构建一个应用程序,但问题是,尽管我遵循了所有要求的说明,但该应用程序一直在给我强制关闭 这里是android部分Android 谷歌地图API v2不工作,android,google-maps,google-maps-api-2,google-maps-android-api-2,Android,Google Maps,Google Maps Api 2,Google Maps Android Api 2,我正在尝试使用GoogleMapsAPIv2构建一个应用程序,但问题是,尽管我遵循了所有要求的说明,但该应用程序一直在给我强制关闭 这里是android部分 import android.support.v4.app.FragmentActivity; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapController; import com.google.android.maps.MapVi
import android.support.v4.app.FragmentActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class GPSonMap extends FragmentActivity{
MapView map;
MapController controller;
LocationManager locationManager;
LocationListener listener;
PendingIntent pendingIntent ;
Criteria criteria;
MapOverlay overlay;
@Override
public void onCreate(Bundle onsaved)
{
super.onCreate(onsaved);
setContentView(R.layout.map);
Log.d("", "entered");
map = (MapView)findViewById(R.id.mapView);
}
这是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" >
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
我面临的另一个问题是,当我尝试import com.google.android.gms.maps.MapFragment代码>
它给出了一个错误,它无法识别包名为com.google.android.gms
,因此我甚至不能使用GoogleMap
或MapFragment
那么,有人能解决这两个问题吗 您可能没有在应用程序项目中添加对Google Play Services库项目的引用。具体请参见标题为“设置项目以使用Google Play services SDK”的部分。我发现您的代码中遗漏了一些内容
答案1:您的GPSonMAP.java必须进行如下修改
public class GPSonMap extends android.support.v4.app.FragmentActivity{
GoogleMap googlemap; // "MapView map;" is wrong
//MapController controller; ==> it was depreciated.
LocationManager locationManager;
LocationListener listener;
PendingIntent pendingIntent ;
Criteria criteria;
//MapOverlay overlay; ==> it was depreciated.
SupportMapFragment fm; // add this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
Log.d("", "entered");
//map = (MapView)findViewById(R.id.mapView); ==> delete it.
fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
googleMap = fm.getMap();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GPSonMap" >
修改上述内容后,按“Ctrl+Shft+(字母表)O”将导入一些必需的类
答案2:您布局中的xml文件必须按如下方式修改
public class GPSonMap extends android.support.v4.app.FragmentActivity{
GoogleMap googlemap; // "MapView map;" is wrong
//MapController controller; ==> it was depreciated.
LocationManager locationManager;
LocationListener listener;
PendingIntent pendingIntent ;
Criteria criteria;
//MapOverlay overlay; ==> it was depreciated.
SupportMapFragment fm; // add this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
Log.d("", "entered");
//map = (MapView)findViewById(R.id.mapView); ==> delete it.
fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
googleMap = fm.getMap();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GPSonMap" >
您的主要活动不是MapsActivity,而是GPSonMap
答案3:您的清单文件与清单文件中描述的包名不匹配。请将包名与相同的包名匹配
将“com.example.example”修改为包名
复制和粘贴一些代码后,您必须至少检查包名和类名。如何在Android上通过GOOGLE Maps API v2显示GOOGLE地图。
请注意设置Google Play服务和清单文件(包括多个权限和API密钥)时,所有这两种情况都是必需的
1。在(支持)片段中显示谷歌地图。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<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"/>
<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout. -->
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
我有这个问题,是在浪费时间(超过1.5天的拉头发挫折)。然后,我观看了一段youtube视频,该视频对向应用程序添加Google Play服务的说明进行了枯燥的演练。在那个视频中,在从SDK管理器添加SDK之后,他们使用Eclipse“检查更新”。这个小小的魔法消失了,下载了一个SDK管理器没有的jar文件!他们真的需要在说明书中添加这一点,以避免人们的沮丧。我很想从我的应用程序中删除谷歌地图。我使用了1.0,需要更新到2.0或沟渠地图。这可能很愚蠢,但我真的不知道如何继续。我将源代码放入文件夹google-play-services_-lib,但当我从properties=>android导入此库时,没有任何东西可供我导入,当我再次运行应用程序时,同样的问题仍然存在。@omar:“我将源代码放入文件夹google-play-services_-lib”我不知道你在这里指的是什么。将google-play-services_lib
项目导入Eclipse,然后从project>Properties>Android>Libraries添加对它的引用。是的,这很好,但是当我要添加对它的引用时,当我单击“add”按钮,此时会出现一个空列表。@OmArHesham:检查导入的google-play-services_lib
项目的Project>Properties>Android,并确保选中“Is Library”复选框。@Commonware…我认为在这种情况下“Is Library”复选框未选中。请,您能解释一下我必须在Java构建路径中检查哪些依赖项吗?
<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout. -->
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
/**
* This shows how to create a simple activity with a raw MapView and add a marker to it. This
* requires forwarding all the important lifecycle methods onto MapView.
*/
public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity {
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}