views:

465

answers:

4

i want show the location in google map... i have address...anybody help me to get the latitude and longitude using the address...

A: 

Try Geocoder.

CommonsWare
It throws the "java.io.IOException service not available"
Kandhasamy
+4  A: 
Geocoder coder = new Geocoder(this);

        List<Address> address;

  try {
            address = coder.getFromLocationName(strAddress,5);
            if (address == null) {
                return null;
            }
            Address location = address.get(0);
            location.getLatitude();
            location.getLongitude();

            p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));

            return p1;

strAddress is string that you pass of address. address variable is address converting and getting address.

ud_an
It throws the "java.io.IOException service not available"
Kandhasamy
You need the right permissions to be able to access the service. #<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.INTERNET" />
Flo
You need connection to the Internet to use the Geocoder.
Flo
which android api version you are building application you need to have google api available i have build with google api 8. check that google api folder is there in your project. and in your manifest file add uses library com.google.android.maps
ud_an
i already gave those permission and include the library...i can get map view...it throws that IOException at geocoder...
Kandhasamy
can you show you logcat error
ud_an
So it does work now? What was the problem?
Flo
the logcat error was09-02 16:32:22.861: ERROR/Exception(722): Service not Available09-02 16:32:22.861: ERROR/Exception(722): java.io.IOException: Service not Available09-02 16:32:22.861: ERROR/Exception(722): at android.location.Geocoder.getFromLocationName(Geocoder.java:159)09-02 16:32:22.861: ERROR/Exception(722): at pack.sample.map.SampleMapApplication.onCreate(SampleMapApplication.java:36)09-02 16:32:22.861: ERROR/Exception(722): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
Kandhasamy
09-02 16:32:22.861: ERROR/Exception(722): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)09-02 16:32:22.861: ERROR/Exception(722): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)09-02 16:32:22.861: ERROR/Exception(722): at android.app.ActivityThread.access$2300(ActivityThread.java:125)09-02 16:32:22.861: ERROR/Exception(722): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)09-02 16:32:22.861: ERROR/Exception(722): at android.os.Handler.dispatchMessage(Handler.java:99)
Kandhasamy
09-02 16:32:22.861: ERROR/Exception(722): at android.os.Looper.loop(Looper.java:123)09-02 16:32:22.861: ERROR/Exception(722): at android.app.ActivityThread.main(ActivityThread.java:4627)09-02 16:32:22.861: ERROR/Exception(722): at java.lang.reflect.Method.invokeNative(Native Method)09-02 16:32:22.861: ERROR/Exception(722): at java.lang.reflect.Method.invoke(Method.java:521)09-02 16:32:22.861: ERROR/Exception(722): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
Kandhasamy
09-02 16:32:22.861: ERROR/Exception(722): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)09-02 16:32:22.861: ERROR/Exception(722): at dalvik.system.NativeStart.main(Native Method)
Kandhasamy
I had to add android.permission.ACCESS_FINE_LOCATION as permission
JP Hellemons
A: 

hi, This is how you can find the latitude and longitude of where we have click on map.

public boolean onTouchEvent(MotionEvent event, MapView mapView) {
//---when user lifts his finger--- if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); }
return false; }

it works well.

To get the location's address we can use geocoder class

Rakesh Gondaliya