[英]Get Current Location with Android Google Map API(v2) but getting Unfortunately application has stopped error
Here is Main Activity.java code : 这是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
}
} }
here is activity_main.xml code : 这是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>
And here is AndroidManifest.xml code : 这是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>
And I'm using Android 4.2.2 and using library google-play-services_lib
I've already search on the Internet and try to fix it for many many times but I still getting this error : Unfortunately application has stopped error. 我正在使用Android 4.2.2和库
google-play-services_lib
我已经在Internet上搜索并尝试对其进行多次修复,但我仍然遇到此错误:不幸的是,应用程序已停止错误。
See here,just change the api key with your key in manifest file and follow these steps: and make sure that generate api key with package name which is mentioned in android manifest file and your google_play_services_lib project should be present in your project's work space only. 看到这里,只需在清单文件中用您的密钥更改api密钥,然后执行以下步骤:并确保生成android清单文件中提到的具有包名的api密钥,并且google_play_services_lib项目应仅出现在项目的工作空间中。
Manifest file: 清单文件:
<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: 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: 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"/>
After got this let me know. 得到这个后让我知道。
And make sure following steps done correct or not: 并确保以下步骤正确无误:
Steps: * to ensure that device has Google Play services APK * to install Google Play Service rev. 步骤:*确保设备具有Google Play服务APK *安装Google Play服务版本。 more than 2
2以上
android.library.reference.1=google-play-services_lib
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
Okay, now you ready to create your own Google Map app with using Google Map APIs V2 for Android. 好的,现在您可以使用适用于Android的Google Map API V2创建自己的Google Map应用了。
If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment. 如果您使用最小SDK = 8创建应用程序,请使用android支持库v4 + SupportMapFragment而不是MapFragment。
Replace your Code 替换您的代码
from 从
SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map1);
to 至
MapFragment fm=(MapFragment)getFragmentManager().findFragmentById(R.id.map1);
Also Add 还添加
<permission
android:name="com.xxxxxx.locationapi.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
For More Details: Visit: Android Maps V2 有关更多详细信息,请访问: Android Maps V2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.