Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

gps - Getting location name using Latitude and Longitude value in android

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...