当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Google Maps API V2 Android - Geocoder Issue

问题描述:

Im using google Maps API V2. In this application im attempting to get the users current location and view it on the map aswell as show the address i.e. street, city, country etc in the textView. So ass the app starts it shows your current location on the map and street address in the textView. Unfortunaly the application just crashes at the moment. Does anybody have an idea of how to resolve this, i will be extremely grateful.

 public class MainActivity extends Activity {

// Google Map

public GoogleMap googleMap;

private TextView textView;

private LocationManager locationManager;

private String bestProvider;

private Location location;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

try {

// Loading map

initilizeMap();

// Changing map type

googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

// Showing / hiding your current location

googleMap.setMyLocationEnabled(true);

// Enable / Disable zooming controls

googleMap.getUiSettings().setZoomControlsEnabled(true);

// Enable / Disable my location button

googleMap.getUiSettings().setMyLocationButtonEnabled(true);

// Enable / Disable Compass icon

googleMap.getUiSettings().setCompassEnabled(true);

// Enable / Disable Rotate gesture

googleMap.getUiSettings().setRotateGesturesEnabled(true);

// Enable / Disable zooming functionality

googleMap.getUiSettings().setZoomGesturesEnabled(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* function to load map If map is not created it will create it for you

* */

private void initilizeMap() {

if (googleMap == null) {

googleMap = ((MapFragment) getFragmentManager().findFragmentById(

R.id.map)).getMap();

// check if map is created successfully or not

if (googleMap == null) {

Toast.makeText(getApplicationContext(),

"Sorry! unable to create maps", Toast.LENGTH_SHORT)

.show();}

}

}

public void onMyLocationChange(Location location) {

//Get current location

LatLng position = new LatLng(location.getLatitude(), location.getLongitude());

//Get address from location

Geocoder geoCoder = new Geocoder(MainActivity.this, Locale.getDefault());

List<Address> addresses;

try {

addresses = geoCoder.getFromLocation(position.latitude, position.longitude, 1);

if (addresses.size()>0){

//Get the first address from the list and get its address lines

Address address = addresses.get(0);

String addressString = "";

for (int i=0;i<address.getMaxAddressLineIndex();i++) {

addressString+=address.getAddressLine(i)+ " ";

}

Toast.makeText(MainActivity.this, addressString, Toast.LENGTH_LONG).show();

}

} catch (IOException e) {

Log.d("GEOCODER", e.getMessage(), e);

}

}

protected void onStart()

{

super.onStart();

this.textView = (TextView)this.findViewById(R.id.textView);

this.locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE);

this.bestProvider = this.locationManager.getBestProvider(criteria,false);

this.location = this.locationManager.getLastKnownLocation(bestProvider);

this.displayLocation(location);

}

@Override

protected void onResume()

{

super.onResume();

this.locationManager.requestLocationUpdates(this.bestProvider,100,0,(LocationListener) this);

initilizeMap();

}

@Override

protected void onPause()

{

super.onPause();

this.locationManager.removeUpdates((LocationListener) this);

}

@SuppressLint("NewApi")

private void displayLocation(Location location)

{

String locality = "";

Geocoder geocoder = new Geocoder(this);

try

{

List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),5);

for(Address address : addresses)

{

if(address.getLocality()!=null)

{

locality = address.getLocality();

break;

}

}

}

catch(IOException ioException)

{

Log.e("MyApp","Exception",ioException);

}

if(!locality.isEmpty())

{

textView.append(location.getLatitude()+", "+location.getLongitude()+" ("+locality+") \n");

}

else

{

textView.append(location.getLatitude()+", "+location.getLongitude()+" ( unknown locality) \n");

}

}

public void onLocationChanged(Location location)

{

this.displayLocation(location);

}

public void onProviderDisabled(String provider)

{

}

public void onProviderEnabled(String provider)

{

}

public void onStatusChanged(String provider, int status, Bundle extras)

{

}

}

Logcat:

04-04 19:54:04.040: W/dalvikvm(27507): threadid=1: thread exiting with uncaught exception (group=0x42014700)

04-04 19:54:04.045: E/AndroidRuntime(27507): FATAL EXCEPTION: main

04-04 19:54:04.045: E/AndroidRuntime(27507): java.lang.RuntimeException: Unable to resume activity {info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: java.lang.ClassCastException: info.androidhive.googlemapsv2.MainActivity cannot be cast to android.location.LocationListener

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread.access$700(ActivityThread.java:159)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.os.Handler.dispatchMessage(Handler.java:99)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.os.Looper.loop(Looper.java:176)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread.main(ActivityThread.java:5419)

04-04 19:54:04.045: E/AndroidRuntime(27507): at java.lang.reflect.Method.invokeNative(Native Method)

04-04 19:54:04.045: E/AndroidRuntime(27507): at java.lang.reflect.Method.invoke(Method.java:525)

04-04 19:54:04.045: E/AndroidRuntime(27507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)

04-04 19:54:04.045: E/AndroidRuntime(27507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)

04-04 19:54:04.045: E/AndroidRuntime(27507): at dalvik.system.NativeStart.main(Native Method)

04-04 19:54:04.045: E/AndroidRuntime(27507): Caused by: java.lang.ClassCastException: info.androidhive.googlemapsv2.MainActivity cannot be cast to android.location.LocationListener

04-04 19:54:04.045: E/AndroidRuntime(27507): at info.androidhive.googlemapsv2.MainActivity.onResume(MainActivity.java:212)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.Activity.performResume(Activity.java:5450)

04-04 19:54:04.045: E/AndroidRuntime(27507): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909)

04-04 19:54:04.045: E/AndroidRuntime(27507): ... 12 more

网友答案:

Your problem is right here:

@Override
protected void onPause()
{
    super.onPause();
    this.locationManager.removeUpdates((LocationListener) this);
}

this is an Activity, not a LocationListener

You need to pass in an actual LocationListener here.

分享给朋友:
您可能感兴趣的文章:
随机阅读: