Java MapFragment使用
我试图在应用程序中的活动中显示地图,请记住,我使用的是Android Studio 0.3.1;切换到Eclipse不是一个选项,除非在Android Studio中这样做是绝对不可能的 我已经学习了多个关于如何做的教程,最近的一个是第二次尝试和,以及类似的问题,例如但不限于和;所有这些都无济于事,下面提到的教程只会在运行时给我一个崩溃,并显示以下消息: E/AndroidRuntime﹕ 致命异常:主 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.maptestproject/com.example.maptestproject.MainActivity}:android.view.InflateException:二进制XML文件行#69:膨胀类片段时出错 很自然,我也尝试检查Google Play API中的示例,但我无法在Android Studio中运行它,因为如果在启动前有Make选项,它会给我一个“output path not specified”错误,或者如果我删除它(在两个地方建议),它会给我一个“APK path not specified”错误 目前,我的测试项目如下,在实际项目中,我的API密钥替换为我的实际API密钥: 清单:Java MapFragment使用,java,android,android-studio,mapfragment,Java,Android,Android Studio,Mapfragment,我试图在应用程序中的活动中显示地图,请记住,我使用的是Android Studio 0.3.1;切换到Eclipse不是一个选项,除非在Android Studio中这样做是绝对不可能的 我已经学习了多个关于如何做的教程,最近的一个是第二次尝试和,以及类似的问题,例如但不限于和;所有这些都无济于事,下面提到的教程只会在运行时给我一个崩溃,并显示以下消息: E/AndroidRuntime﹕ 致命异常:主 java.lang.RuntimeException:无法启动activity Compon
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maptestproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.maptestproject.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<permission
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.maptestproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.MY_API_KEY"
android:value="MY_API_KEY"/>
</application>
</manifest>
package com.example.maptestproject;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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 MainActivity extends 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_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
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)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
活动\u main.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maptestproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.maptestproject.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<permission
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.maptestproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.MY_API_KEY"
android:value="MY_API_KEY"/>
</application>
</manifest>
package com.example.maptestproject;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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 MainActivity extends 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_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
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)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
更具体地说,该应用程序需要显示一个特定的位置,然后跟踪从用户当前位置到标记位置的路径(或者用谷歌地图发送到网络视图,或者打开设备的gps,任何更简单的方法),但只要能让这件事顺利进行,任何输入都将受到极大的欢迎
我很抱歉问了这么长的问题,但这件事已经困扰了我将近两天了