以下code我写来获得位置名称
包demo.gps.locname;
进口java.io.IOException异常;
进口的java.util.List;
进口java.util.Locale中;
//进口com.VertexVerveInc.GPSLocator.R;
//进口com.VertexVerveInc.GPSLocator.GPSLocatorActivity.GPSLocationListener;
//进口com.VertexVerveInc.GPSLocator.GPSLocatorActivity.MapOverlay;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
//进口com.google.android.maps.Overlay;
//进口android.app.Activity;
进口android.content.Context;
进口android.location.Address;
进口android.location.Geo codeR;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
//进口android.util.Log;
进口android.widget.TextView;
进口android.widget.Toast;
公共类GPSLocationActivity扩展MapActivity {
/ **第一次创建活动时调用。 * /
私人LocationManager locationManager;
私人LocationListener的LocationListener的;
私人TextView的电视;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的=新GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,
0,LocationListener的);
电视=(TextView中)findViewById(R.id.tv);
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
私有类GPSLocationListener实现LocationListener的
{
@覆盖
公共无效onLocationChanged(位置定位){
如果(位置!= NULL){
的GeoPoint点=新的GeoPoint(
(中间体)(location.getLatitude()* 1E6),
(中间体)(location.getLongitude()* 1E6));
/ * Toast.makeText(getBaseContext(),
纬度:+ location.getLatitude()+
经度:+ location.getLongitude(),
Toast.LENGTH_SHORT).show(); * /
// mapController.animateTo(点);
// mapController.setZoom(16);
//添加标记
// MapOverlay mapOverlay =新MapOverlay();
// mapOverlay.setPointToDraw(点);
//名单,其中,叠加> listOfOverlays =调用MapView.getOverlays();
// listOfOverlays.clear();
// listOfOverlays.add(mapOverlay);
字符串的地址= ConvertPointToLocation(点);
Toast.makeText(getBaseContext(),地址,Toast.LENGTH_SHORT).show();
tv.setText(地址);
// mapView.invalidate();
}
}
公共字符串ConvertPointToLocation(的GeoPoint点){
字符串的地址=;
地理codeR地理codeR =新的地缘codeR(
getBaseContext(),Locale.getDefault());
尝试 {
名单<地址>地址=地理coder.getFromLocation(
point.getLatitudeE6()/ 1E6,
point.getLongitudeE6()/ 1E6,1);
如果(addresses.size()大于0){
对于(INT指数= 0;指数< addresses.get(0).getMaxAddressLineIndex();指数++)
地址+ = addresses.get(0).getAddressLine(指数)+;
}
}
赶上(IOException异常E){
e.printStackTrace();
}
退货地址;
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}
}
}
我收到以下错误
01-23 13:20:26.103:W / dalvikvm(231):无法解析Ldemo / GPS / locname / GPSLocationActivity的父类; (15)
01-23 13:20:26.142:W / dalvikvm(231):类的链接Ldemo / GPS / locname / GPSLocationActivity;失败
01-23 13:20:26.142:D / AndroidRuntime(231):关闭虚拟机
01-23 13:20:26.142:W / dalvikvm(231):主题ID = 3:线程退出与未捕获的异常(组= 0x4001aa28)
01-23 13:20:26.142:E / AndroidRuntime(231):未捕获的处理程序:由于未捕获的异常螺纹主力退出
01-23 13:20:26.173:E / AndroidRuntime(231):java.lang.RuntimeException的:无法实例活动ComponentInfo {demo.gps.locname / demo.gps.locname.GPSLocationActivity}:抛出java.lang.ClassNotFoundException:演示.gps.locname.GPSLocationActivity装载机dalvik.system.PathClassLoader@4376ac28
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.ActivityThread.access $ 2100(ActivityThread.java:116)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1794)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.os.Looper.loop(Looper.java:123)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.ActivityThread.main(ActivityThread.java:4203)
01-23 13:20:26.173:E / AndroidRuntime(231):在java.lang.reflect.Method.invokeNative(本机方法)
01-23 13:20:26.173:E / AndroidRuntime(231):在java.lang.reflect.Method.invoke(Method.java:521)
01-23 13:20:26.173:E / AndroidRuntime(231):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
01-23 13:20:26.173:E / AndroidRuntime(231):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
01-23 13:20:26.173:E / AndroidRuntime(231):在dalvik.system.NativeStart.main(本机方法)
01-23 13:20:26.173:E / AndroidRuntime(231):抛出java.lang.ClassNotFoundException:产生的原因demo.gps.locname.GPSLocationActivity装载机dalvik.system.PathClassLoader@4376ac28
01-23 13:20:26.173:E / AndroidRuntime(231):在dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-23 13:20:26.173:E / AndroidRuntime(231):在java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-23 13:20:26.173:E / AndroidRuntime(231):在java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.Instrumentation.newActivity(Instrumentation.java:1097)
01-23 13:20:26.173:E / AndroidRuntime(231):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
01-23 13:20:26.173:E / AndroidRuntime(231):11 ...更多
01-23 13:20:26.203的:I / dalvikvm(231):主题ID = 7:反应以信号3
01-23 13:20:26.203:E / dalvikvm(231):无法打开堆栈跟踪文件/data/anr/traces.txt':权限被拒绝
的Manifest.xml
<使用-SDK安卓的minSdkVersion =4/>
<使用-权限的Android:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名称=。GPSLocationActivity
机器人:标签=@字符串/ APP_NAME>
<使用库机器人:名称=com.google.android.maps/>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
解决方案
在
<使用库机器人:名称=com.google.android.maps/>
应该包含在<应用>
节点,而不是<活性GT;
节点。这应该解决的问题,因为截至目前为止,你是不是要求对地图的权限。
I have written following code to get location name
package demo.gps.locname;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
//import com.VertexVerveInc.GPSLocator.R;
//import com.VertexVerveInc.GPSLocator.GPSLocatorActivity.GPSLocationListener;
//import com.VertexVerveInc.GPSLocator.GPSLocatorActivity.MapOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
//import com.google.android.maps.Overlay;
//import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
//import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class GPSLocationActivity extends MapActivity {
/** Called when the activity is first created. */
private LocationManager locationManager;
private LocationListener locationListener;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
tv =(TextView)findViewById(R.id.tv);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
/* Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();*/
// mapController.animateTo(point);
// mapController.setZoom(16);
// add marker
// MapOverlay mapOverlay = new MapOverlay();
// mapOverlay.setPointToDraw(point);
// List<Overlay> listOfOverlays = mapView.getOverlays();
// listOfOverlays.clear();
// listOfOverlays.add(mapOverlay);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
tv.setText(address);
// mapView.invalidate();
}
}
public String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
I am getting following error
01-23 13:20:26.103: W/dalvikvm(231): Unable to resolve superclass of Ldemo/gps/locname/GPSLocationActivity; (15)
01-23 13:20:26.142: W/dalvikvm(231): Link of class 'Ldemo/gps/locname/GPSLocationActivity;' failed
01-23 13:20:26.142: D/AndroidRuntime(231): Shutting down VM
01-23 13:20:26.142: W/dalvikvm(231): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
01-23 13:20:26.142: E/AndroidRuntime(231): Uncaught handler: thread main exiting due to uncaught exception
01-23 13:20:26.173: E/AndroidRuntime(231): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{demo.gps.locname/demo.gps.locname.GPSLocationActivity}: java.lang.ClassNotFoundException: demo.gps.locname.GPSLocationActivity in loader dalvik.system.PathClassLoader@4376ac28
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.os.Looper.loop(Looper.java:123)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.ActivityThread.main(ActivityThread.java:4203)
01-23 13:20:26.173: E/AndroidRuntime(231): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:20:26.173: E/AndroidRuntime(231): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 13:20:26.173: E/AndroidRuntime(231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-23 13:20:26.173: E/AndroidRuntime(231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
01-23 13:20:26.173: E/AndroidRuntime(231): at dalvik.system.NativeStart.main(Native Method)
01-23 13:20:26.173: E/AndroidRuntime(231): Caused by: java.lang.ClassNotFoundException: demo.gps.locname.GPSLocationActivity in loader dalvik.system.PathClassLoader@4376ac28
01-23 13:20:26.173: E/AndroidRuntime(231): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-23 13:20:26.173: E/AndroidRuntime(231): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-23 13:20:26.173: E/AndroidRuntime(231): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
01-23 13:20:26.173: E/AndroidRuntime(231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
01-23 13:20:26.173: E/AndroidRuntime(231): ... 11 more
01-23 13:20:26.203: I/dalvikvm(231): threadid=7: reacting to signal 3
01-23 13:20:26.203: E/dalvikvm(231): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Manifest.xml
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".GPSLocationActivity"
android:label="@string/app_name" >
<uses-library android:name="com.google.android.maps" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
解决方案
The
<uses-library android:name="com.google.android.maps" />
should be contained in the <application>
node and not the <activity>
node. That should fix the problem because as of now, you are not requesting for the maps permission.