在android中使用纬度和经度值获取位置名称

时间:2012-01-23 07:59:22

标签: android gps

我写了以下代码来获取位置名称

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) {
        }
    }
}

我收到以下错误

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>

2 个答案:

答案 0 :(得分:4)

<uses-library android:name="com.google.android.maps" />

应包含在<application>节点中,而不是<activity>节点中。这应该可以解决问题,因为到目前为止,您还没有请求地图权限。

答案 1 :(得分:1)

嘿,你只需使用此代码Android Location based services 在此代码中,您将LocationManager.GPS_PROVIDER更改为LocationManager.Network_PROVIDER. 一切顺利