i'm trying to load locations from the contact's list inside the android phone to google maps. whenever the user clicks on the button, it will load all the contacts into google maps and place a marker on each contact's location.
i already figured out how to get the contact's address but i am having trouble just looping and placing the markers.
for (int i = 0; i < addresslist.size(); i++) {
String contact = namelist.get(i).toString();
StringTokenizer tokens = new StringTokenizer(contact, ",");
String name = tokens.nextToken();
String street = tokens.nextToken();
String city = tokens.nextToken();
try {
List<Address> a = geoCoder.getFromLocationName(street + ", "
+ city, 1);
double lat = a.get(0).getLatitude();
double lon = a.get(0).getLongitude();
LatLng location = new LatLng(lat, lon);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions().position(location).title(name));
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,15));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have name, address and city stored in a list, it separates them into separate variables. it takes the address and city, converts them into longitude and latitude and then places the markers on the map.
I'm having trouble trying to get it work. It just crashes and giving me errors, the try and catch portion works fine by itself, if i hard code the address in. I'm all out of ideas and I spent 2-3 hours trying to figure out why. Am I doing this wrong? or is there a better solution doing this.
edit - here;s my logcat
05-28 03:23:19.575: E/AndroidRuntime(8632): FATAL EXCEPTION: main 05-28 03:23:19.575: E/AndroidRuntime(8632): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app1project/com.example.app1project.ContactsMapActivity}: java.util.NoSuchElementException 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.access$700(ActivityThread.java:139) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.os.Handler.dispatchMessage(Handler.java:99) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.os.Looper.loop(Looper.java:137) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.main(ActivityThread.java:4918) 05-28 03:23:19.575: E/AndroidRuntime(8632): at java.lang.reflect.Method.invokeNative(Native Method) 05-28 03:23:19.575: E/AndroidRuntime(8632): at java.lang.reflect.Method.invoke(Method.java:511) 05-28 03:23:19.575: E/AndroidRuntime(8632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 05-28 03:23:19.575: E/AndroidRuntime(8632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 05-28 03:23:19.575: E/AndroidRuntime(8632): at dalvik.system.NativeStart.main(Native Method) 05-28 03:23:19.575: E/AndroidRuntime(8632): Caused by: java.util.NoSuchElementException 05-28 03:23:19.575: E/AndroidRuntime(8632): at java.util.StringTokenizer.nextToken(StringTokenizer.java:208) 05-28 03:23:19.575: E/AndroidRuntime(8632): at com.example.app1project.ContactsMapActivity.onCreate(ContactsMapActivity.java:79) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.Activity.performCreate(Activity.java:5048) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052) 05-28 03:23:19.575: E/AndroidRuntime(8632): ... 11 more