Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Android
how to get a longitude & latitude from a given address
shawn peter
Ranch Hand
Posts: 1325
1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I need to get a longitude & latitude from a given address ?
can any one tell me ?
Paul Clapham
Marshal
Posts: 28193
95
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
There are web services you can contact for this sort of information. The one we use where I work belongs to Google:
The Google Geocoding API
. The
word
for what you want to do is "geocoding".
Tim Moores
Saloon Keeper
Posts: 7585
176
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That's what the
android.location.Geocoder
class does.
shawn peter
Ranch Hand
Posts: 1325
1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
this is my
java
class
package com.find; import android.app.Activity; import android.os.Bundle; import java.io.IOException; import java.util.List; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; public class GetPlace extends MapActivity { Geocoder geocoder = null; MapView mapView = null; @Override protected boolean isLocationDisplayed() { return false; } @Override protected boolean isRouteDisplayed() { return false; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.geoMap); mapView.setBuiltInZoomControls(true); // lat/long of Jacksonville, FL int lat = (int) (30.334954 * 1000000); int lng = (int) (-81.5625 * 1000000); GeoPoint pt = new GeoPoint(lat, lng); mapView.getController().setZoom(10); mapView.getController().setCenter(pt); geocoder = new Geocoder(this); } public void doClick(View arg0) { try { EditText loc = (EditText) findViewById(R.id.location); String locationName = loc.getText().toString(); List<Address> addressList = geocoder.getFromLocationName( locationName, 5); if (addressList != null && addressList.size() > 0) { int lat = (int) (addressList.get(0).getLatitude() * 1000000); int lng = (int) (addressList.get(0).getLongitude() * 1000000); GeoPoint pt = new GeoPoint(lat, lng); mapView.getController().setZoom(15); mapView.getController().setCenter(pt); } } catch (IOException e) { e.printStackTrace(); } }
this is my main xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:layout_height="wrap_content" android:text="EditText" android:layout_width="match_parent" android:id="@+id/location"></EditText> <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/geoMap" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:enabled="true" android:apiKey="0Tvn4iCb4u0SYc_tCFFM61j2dWjpahP8ETnKhcA" /> </LinearLayout>
this is the exception i am getting
09-30 22:52:02.834: DEBUG/AndroidRuntime(274): Shutting down VM 09-30 22:52:02.834: WARN/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): FATAL EXCEPTION: main 09-30 22:52:02.874: ERROR/AndroidRuntime(274): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.draw/com.draw.FindLocation}: java.lang.ClassNotFoundException: com.draw.FindLocation in loader dalvik.system.PathClassLoader[/data/app/com.draw-1.apk] 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): Caused by: java.lang.ClassNotFoundException: com.draw.FindLocation in loader dalvik.system.PathClassLoader[/data/app/com.draw-1.apk] 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 09-30 22:52:02.874: ERROR/AndroidRuntime(274): ... 11 more
}
Tim Moores
Saloon Keeper
Posts: 7585
176
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
So Android can't find the com.draw.FindLocation class - is it part of the .apk file? It's not part of Android itself.
shawn peter
Ranch Hand
Posts: 1325
1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I create a new project
this is my java class
package com.arunaapplication; import android.app.Activity; import android.os.Bundle; import java.io.IOException; import java.util.List; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; public class ArunaActivity extends MapActivity { Geocoder geocoder = null; MapView mapView = null; @Override protected boolean isLocationDisplayed() { return false; } @Override protected boolean isRouteDisplayed() { return false; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.geocode); mapView = (MapView) findViewById(R.id.geoMap); mapView.setBuiltInZoomControls(true); // lat/long of Jacksonville, FL int lat = (int) (30.334954 * 1000000); int lng = (int) (-81.5625 * 1000000); GeoPoint pt = new GeoPoint(lat, lng); mapView.getController().setZoom(10); mapView.getController().setCenter(pt); geocoder = new Geocoder(this); } public void doClick(View arg0) { try { EditText loc = (EditText) findViewById(R.id.location); String locationName = loc.getText().toString(); List<Address> addressList = geocoder.getFromLocationName( locationName, 5); if (addressList != null && addressList.size() > 0) { int lat = (int) (addressList.get(0).getLatitude() * 1000000); int lng = (int) (addressList.get(0).getLongitude() * 1000000); GeoPoint pt = new GeoPoint(lat, lng); mapView.getController().setZoom(15); mapView.getController().setCenter(pt); } } catch (IOException e) { e.printStackTrace(); } } }
this is main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:text="EditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/location"></EditText> </LinearLayout>
this is geocodexml file
<?xml version="1.0" encoding="utf-8"?> <!-- This file is /res/layout/geocode.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:layout_width="fill_parent" android:id="@+id/location" android:layout_height="wrap_content" android:text="White House" /> <Button android:id="@+id/geocodeBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:text="Find Location" /> </LinearLayout> <com.google.android.maps.MapView android:id="@+id/geoMap" android:clickable="true" android:layout_width="fill_parent" android:layout_height="320px" android:apiKey="0Tvn4iCb4u0SYc_tCFFM61j2dWjpahP8ETnKhcA" /> </RelativeLayout>
this is th\e error
10-01 08:41:32.854: ERROR/AndroidRuntime(325): FATAL EXCEPTION: main 10-01 08:41:32.854: ERROR/AndroidRuntime(325): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.arunaapplication/com.arunaapplication.ArunaActivity}: java.lang.ClassNotFoundException: com.arunaapplication.ArunaActivity in loader dalvik.system.PathClassLoader[/data/app/com.arunaapplication-1.apk] 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.os.Handler.dispatchMessage(Handler.java:99) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.os.Looper.loop(Looper.java:123) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.ActivityThread.main(ActivityThread.java:4627) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at java.lang.reflect.Method.invokeNative(Native Method) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at java.lang.reflect.Method.invoke(Method.java:521) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at dalvik.system.NativeStart.main(Native Method) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): Caused by: java.lang.ClassNotFoundException: com.arunaapplication.ArunaActivity in loader dalvik.system.PathClassLoader[/data/app/com.arunaapplication-1.apk] 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 10-01 08:41:32.854: ERROR/AndroidRuntime(325): ... 11 more
Michal Krawczyk
Greenhorn
Posts: 3
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is working code:
SearchByAddress.java
package pl.kravczoo.android; import java.util.List; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; public class SearchByAddress extends MapActivity { MapView mapView = null; Geocoder geocoder = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view); mapView = (MapView) findViewById(R.id.geoMap); mapView.setBuiltInZoomControls(true); mapView.setClickable(true); // map starting point int lat = (int) (51.678383 * 1000000); int lng = (int) (19.334822 * 1000000); GeoPoint pt = new GeoPoint(lat, lng); mapView.getController().setZoom(18); mapView.getController().setCenter(pt); Button geoBtn = (Button) findViewById(R.id.geocodeBtn); geocoder = new Geocoder(this); geoBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { EditText locale = (EditText) findViewById(R.id.location); String locationName = locale.getText().toString(); List<Address> addressList = geocoder.getFromLocationName( locationName, 5); if (addressList != null && addressList.size() > 0) { int lat = (int) (addressList.get(0).getLatitude() * 1e6); int lng = (int) (addressList.get(0).getLongitude() * 1e6); TextView pozycja = (TextView) findViewById(R.id.position); pozycja.setText("lat: " + addressList.get(0).getLatitude() + " lng: " + addressList.get(0).getLongitude()); GeoPoint pt = new GeoPoint(lat, lng); mapView.getController().setZoom(18); mapView.getController().setCenter(pt); } } catch (Exception e) { e.printStackTrace(); } } }); } @Override public boolean isLocationDisplayed() { return false; } @Override public boolean isRouteDisplayed() { return false; } }
view.xml
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"> <EditText android:id="@+id/location" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Address here..." /> <Button android:id="@+id/geocodeBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Search" /> <TextView android:id="@+id/position" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Latitude/Longitude..." /> </LinearLayout> <com.google.android.maps.MapView android:id="@+id/geoMap" android:layout_width="fill_parent" android:layout_height="320px" android:clickable="true" android:apiKey="your-api-key" /> </RelativeLayout>
shawn peter
Ranch Hand
Posts: 1325
1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I try this with your example .then it gives this error.please see attachment.
error.jpg
Maneesh Godbole
Bartender
Posts: 11497
19
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Instead of posting attachments, it would help if you copy paste the stack trace
[
How to ask questions
] [
Donate a pint, save a life!
] [
Onff-turn it on!
]
Michal Krawczyk
Greenhorn
Posts: 3
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I run my code which I paste above and everything is ok.
Execution result is on the image
placed here
Your console says there is a problem with your device. You tried run application once or maybe more?
You can try choose your Android API version 1.6, I know that can help sometimes.
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
need to find distance between given two cities
Automatic TimeZone corrections
find location of google maps by giving address
Having issues reRendering a map when a rich:tree node is clicked on(SOLVED)
is there any way to trigger onLocationChanged other than moving
More...