我按照以下步骤创建了一个带有API V2的Google Map:
我获得了de API密钥并在三星Galaxy S3(真实设备)上进行了测试
这是Android Manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.localizaciongooglemapv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="android.localizaciongooglemapv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="android.localizaciongooglemapv2.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"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="android.localizaciongooglemapv2.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.API_KEY"
android:value="MyAPIKey"/> <!--I'm sure It's correct-->
<!-- <meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />--> <!-- I thought this was the problem but It is not-->
</application>
这是我的主要活动:
package android.localizaciongooglemapv2;
import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的activity_main.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=".MainActivity" >
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tv_location"
class="com.google.android.gms.maps.SupportMapFragment" />
我在错误日志中得到了这个:
11-06 12:09:03.714: D/AndroidRuntime(24366): Shutting down VM
11-06 12:09:03.714: W/dalvikvm(24366): threadid=1: thread exiting with uncaught exception (group=0x416842a0)
11-06 12:09:03.719: E/AndroidRuntime(24366): FATAL EXCEPTION: main
11-06 12:09:03.719: E/AndroidRuntime(24366): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.localizaciongooglemapv2/android.localizaciongooglemapv2.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.os.Looper.loop(Looper.java:137)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-06 12:09:03.719: E/AndroidRuntime(24366): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:09:03.719: E/AndroidRuntime(24366): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-06 12:09:03.719: E/AndroidRuntime(24366): at dalvik.system.NativeStart.main(Native Method)
11-06 12:09:03.719: E/AndroidRuntime(24366): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.Activity.setContentView(Activity.java:1924)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.localizaciongooglemapv2.MainActivity.onCreate(MainActivity.java:27)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.Activity.performCreate(Activity.java:5206)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-06 12:09:03.719: E/AndroidRuntime(24366): ... 11 more
11-06 12:09:03.719: E/AndroidRuntime(24366): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.maps.internal.q.v(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.maps.internal.q.u(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.maps.SupportMapFragment$b.cE(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.dynamic.a.a(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
11-06 12:09:03.719: E/AndroidRuntime(24366): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-06 12:09:03.719: E/AndroidRuntime(24366): ... 21 more
11-06 12:09:03.749: I/Process(24366): Sending signal. PID: 24366 SIG: 9
我到处搜索了很多信息,这里是在stackoverflow或链接的评论中,我无法得到解决方案。你可以帮帮我吗??非常感谢!!
答案 0 :(得分:1)
您无法两次声明架构。片段声明应该与activity_main.xml相似:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在Manifest文件中,您必须添加有关gms版本的元信息。因此,在应用程序标记
中添加API_KEY以及gms版本<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR MAP API KEY HERE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
祝你好运...
答案 1 :(得分:0)
这就是加载地图所需的全部内容:
private void setUpMap() {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
}
的xml:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
有关详细信息,请按照以下精彩教程进行操作:
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/