使用Android Google Map API(v2)获取当前位置,但获得遗憾的是应用程序已停止错误

时间:2013-12-18 04:30:07

标签: java android google-maps-api-2

这是Main Activity.java代码:

package com.example.vd;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;

import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;

public class MainActivity extends FragmentActivity implements LocationListener {


GoogleMap googleMap;
LatLng myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map1);
    googleMap=fm.getMap();
    googleMap.setMyLocationEnabled(true);
    LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
    Criteria criteria=new Criteria();
    String provider =locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);
    if(location!=null){
        double latitude =location.getLatitude();
        double longitude=location.getLongitude();
        LatLng latlng=new LatLng(latitude, longitude);
        myPosition=new LatLng(latitude, longitude);
        googleMap.addMarker(new MarkerOptions().position(myPosition).title("Here"));
    }





}

@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;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@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

}

}

这里是activity_main.xml代码:

<?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/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

这是AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vd"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="14" />
<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"/>
<uses-permission android:name="com.example.vd.permission.MAPS_RECEIVE"/>
<permission android:name="com.example.vd.permission.MAPS_RECEIVE"
     />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.vd.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="AIzaSyB5pN0xyJTRLVOAX3scagfb58b0bB0TBwg"/>
</application>

我正在使用Android 4.2.2并使用库google-play-services_lib 我已经在互联网上搜索并尝试多次修复它但我仍然收到此错误:遗憾的是应用程序已停止错误。

2 个答案:

答案 0 :(得分:0)

请参阅此处,只需使用清单文件中的密钥更改api密钥,然后按照以下步骤操作: 并且确保使用android清单文件中提到的包名称生成api密钥,并且google_play_services_lib项目应仅存在于项目的工作空间中。

清单文件:              

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <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.geeklabs.map.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="replace with your API key"/>

    </application>

</manifest>

MainActivity.java:

    package com.geeklabs.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

activity_main.xml中:

    <?xml version="1.0" encoding="utf-8"?>
    <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:name="com.google.android.gms.maps.MapFragment"/>

得到这个后告诉我。

确保以下步骤正确无误:

步骤: *确保该设备具有Google Play服务APK *安装Google Play服务版。超过2

enter image description here

  • https://code.google.com/apis/console/
  • 创建项目
  • 启用“Google Maps Android API v2” enter image description here
  • 在API控制台注册项目中的SHA1(现在,你需要写SHA1;你的.app.package.name )并获取API KEY
  • 将目录ANDROID_SDK_DIR / extras / google / google_play_services / libproject / google-play-services_lib复制到项目的根目录
  • 将下一行添加到YOUR_PROJECT / project.properties

android.library.reference.1=google-play-services_lib

  • 将下一行添加到YOUR_PROJECT / proguard-project.txt

-keep class * extends java.util.ListResourceBundle {

protected Object[][] getContents();

}

好的,现在您已准备好使用适用于Android的Google Map API V2创建自己的Google地图应用。

如果使用min SDK = 8创建应用程序,请使用android支持库v4 + SupportMapFragment而不是MapFragment。

答案 1 :(得分:0)

替换您的代码

来自

SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map1);

MapFragment fm=(MapFragment)getFragmentManager().findFragmentById(R.id.map1);

同时添加

<permission
        android:name="com.xxxxxx.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

更多详情: 访问:Android Maps V2