谷歌地图API V2 Android - 地理编码器问题

时间:2014-04-04 18:26:49

标签: java android google-maps location geocode

我正在使用谷歌地图API V2。在此应用程序中,我试图获取用户当前位置并在地图上查看它以及在textView中显示地址,即街道,城市,国家等。因此,应用程序启动它会在地图上显示您当前的位置,并在textView中显示街道地址。不幸的是,应用程序此刻崩溃了。有没有人知道如何解决这个问题,我将非常感激。

  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

1 个答案:

答案 0 :(得分:0)

你的问题就在这里:

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

thisActivity,而不是LocationListener

您需要在此处传递实际的LocationListener