0
我正在使用Google Maps API V2。在這個應用程序中,我試圖獲取用戶當前位置並在地圖上查看它,並在textView中顯示地址,即街道,城市,國家等。所以驢應用程序啓動它顯示您的當前位置的地圖和textView中的街道地址。不幸的是,此時應用程序崩潰。有誰知道如何解決這個問題,我會非常感激。Google Maps API V2 Android - 地理編碼問題
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
你可以發佈堆棧跟蹤嗎? – kmdupr33
我的方法看起來是否正確 – user3247335