MapActivity未显示列表

时间:2013-05-04 09:37:27

标签: android listview

我正在开发一个场景,我必须在列表视图中显示最近位置的地址,该地址位于 MapActivity 上。但在成功运行我的代码后,列表将不会出现在 MapActivity

这是我的代码:

public class GMapActivity extends com.google.android.maps.MapActivity implements OnClickListener{

    MapView _mapView;
    private static  int latitudeE6 = 37985339;
    private static  int longitudeE6 = 23716735;
    private GpsListener _gpsLister=new GpsListener();
    List<Overlay> overlayList;

    MapController controller;

    Button _cancelBtn;

    String returnAddress = "";

    ListView locationListView;
    ArrayList<String> locationAddressArrayList=new ArrayList<String>();
    ArrayAdapter<String> locationAdapter;

    LocationListAdapter locationListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapscreen);

        _mapView=(MapView)findViewById(R.id.mapView);
        _mapView.setBuiltInZoomControls(true);

        _cancelBtn=(Button)findViewById(R.id.cancel_button);
        _cancelBtn.setOnClickListener(this);

        locationListView=(ListView)findViewById(R.id.location_listview);
        /*locationAdapter=new ArrayAdapter<String>(GMapActivity.this, android.R.layout.simple_list_item_1, locationAddressArrayList);*/
        locationListAdapter=new LocationListAdapter(GMapActivity.this, locationAddressArrayList);
        locationAddressArrayList.add("RNT Marg");
        locationAddressArrayList.add("RNT Marg");
        locationAddressArrayList.add("RNT Marg");
        locationAddressArrayList.add("RNT Marg");
        locationAddressArrayList.add("RNT Marg");
        locationAddressArrayList.add("RNT Marg");
        locationListView.setAdapter(locationListAdapter);
        ///////////////START GPS/////////////////
        if(Constant.isGpsStarted==false){
            startGPS();
            latitudeE6=(int)Constant.latitudeValue;
            longitudeE6=(int)Constant.longitudeValue;
        }
        //GetAddress(Constant.latitudeValue, Constant.longitudeValue);
        /////////////////////////////////////////

        ////////////////////////////////GET NEAREST LOCATIONS/////////////////////////////
        LocationManager locationManager = (LocationManager) GMapActivity.this
                .getSystemService(Context.LOCATION_SERVICE);
        LocationListener    locationListener = new LocationListener() {
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }

            public void onLocationChanged(Location location) {
                Double l1 = location.getLatitude();
                Double l2 = location.getLongitude();
                //GetAddress(l1, l2);
            }
        };

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);  
        //////////////////////////////////////////////////////////////////////////////////
        List mapOverlays = _mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.mapping);
        CustomItemizedOverlay itemizedOverlay =
                new CustomItemizedOverlay(drawable, this);

        GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
        OverlayItem overlayitem =
                new OverlayItem(point, ""+Constant.latitudeValue, ""+Constant.longitudeValue);

        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        MapController mapController = _mapView.getController();

        mapController.animateTo(point);
        mapController.setZoom(6);

        /*overlayList = _mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.mapping);
        CustomPinPoint itemizedoverlay = new CustomPinPoint(drawable,this);


        double lat_coordinates[] ={27.700556,28.2642635,30.0018168,29.776669,29.4096819,29.4560611};
        double lng_coordinates[] ={85.3630,83.9735195,80.7382742,81.2518833,81.8115051,80.5403779};
        String place_name[] ={"kathmandu","Pokhara","Darchula","Bajhang","Bajura","Baitadi"};
        String place_info[] ={"Its an capital of Nepal","Its and tourist place of Nepal","Its one of the beautiful place in country side","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 47/47","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 7/7","CHD District Target:10 21,960, VDCs/Muncipalities reported:44/41","CHD District Target: 33,3123, VDCs/Muncipalities reported: 47/47"};

        try{
            for(int i=0; i<place_name.length; i++)
            {
                GeoPoint point = new GeoPoint((int)(lat_coordinates[i]*1E6),(int)(lng_coordinates[i]*1E6));
                OverlayItem overlayitem = new OverlayItem(point, place_name[i], place_info[i]);
                itemizedoverlay.addOverlay(overlayitem);
            }
        }catch(NullPointerException e){
            e.getStackTrace();

        }
        finally{
            overlayList.add(itemizedoverlay);
        }

        controller = _mapView.getController();
        controller.animateTo(new GeoPoint((int)(lat_coordinates[0]*1E6),(int)(lng_coordinates[0]*1E6)));
        controller.setZoom(8);*/
    }

    //START GPS
    public void startGPS(){
        Constant.isGpsStarted = true;
        _gpsLister.startGpsCallBack(GMapActivity.this);
        _gpsLister.stopGpsCallBack();
        _gpsLister.startGpsCallbackAgain(GMapActivity.this);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.cancel_button:
            this.finish();
            break;

        default:
            break;
        }
    }

    public void getNearestLocations(){

    }

    private void GetAddress(final Double lat, final Double lon) {
        Constant.progressDialog = ProgressDialog.show(GMapActivity.this,"", "Please wait...");
        new Thread(){
            public void run() {
                Geocoder geocoder = new Geocoder(GMapActivity.this, Locale.ENGLISH);
                List<Address> addresses = null;
                try {
                    addresses = geocoder.getFromLocation(lat, lon, 1);
                    if (!addresses.equals(null)) {
                        Address returnedAddress = addresses.get(0);
                        StringBuilder strReturnedAddress = new StringBuilder("\n");
                        for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {

                            locationAddressArrayList.add((String)returnedAddress.getAddressLine(i));

                            strReturnedAddress
                            .append(returnedAddress.getAddressLine(i)).append(
                                    "\n");
                        }
                        returnAddress = "Around: " + strReturnedAddress.toString();
                    } else {
                        returnAddress = "No Address returned!";
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    returnAddress = "Location: https://maps.google.co.in/maps?hl=en&q=" + lat
                            + "," + lon;
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    returnAddress = lat + "," + lon;
                }

                runOnUiThread(new Runnable() {
                    public void run() {
                        locationAdapter.notifyDataSetChanged();
                        locationListView.setAdapter(locationAdapter);
                        Constant.progressDialog.cancel();
                    }
                });
            };
        }.start();

        //  return returnAddress;
    }
}

1 个答案:

答案 0 :(得分:2)

不推荐使用此库。

  

Google Maps Android API版本1已于2012年12月3日正式弃用。这意味着从2013年3月18日起,您将无法再为此版本请求API密钥。 Google Maps Android API v1不会添加任何新功能。但是,使用v1的应用程序将继续在设备上运行。鼓励现有和新开发人员使用Google Maps Android API v2