2

我正在尝试将 GoogleMapview和 aListView合二为一activityMapview应取2/3上侧,列表下侧的 1/3(纵向)。

以这种方式假设它是肖像:http: //i.stack.imgur.com/IoGjr.png

MapView和,都ListView将收到相同的JSON信息,他们将得到onCreate()。我已经能够适应屏幕,并获取它获取位置的MapView数据。但我希望我得到的位置也显示在上面有人可以告诉我,如何显示吗?2/3ListViewlistViewmaplocationmap

这是我的代码:

// 将请求设为私有的 url 静态String url = "";//这里传递url

// JSON Node names
    private static final String TAG_LOCATION = "location";
    private static final String TAG_LOCATION1= "location";
    private static final String TAG_LOCATION_ID = "LocationID";
    private static final String TAG_NAME = "Name";
    private static final String TAG_PHONE = "Phone";
    private static final String TAG_FORMATTED_PHONE = "FormattedPhone";
    private static final String TAG_ADDRESS = "Address";
    private static final String TAG_CROSS_STREET = "CrossStreet";
    private static final String TAG_LAT = "Lat";
    private static final String TAG_LNG = "Lng";
    private static final String TAG_DISTANCE= "Distance";
    private static final String TAG_POSTAL_CODE = "PostalCode";
    private static final String TAG_CITY = "City";
    private static final String TAG_STATE = "State";
    private static final String TAG_COUNTRY= "Country";

 // contacts JSONArray
    JSONArray location = null;
    JSONObject location1=null;

    ListView locationList;
    private MapView mapView;
    LocationManager lm;
    LocationListener locationListener;
    MapController mapController;

    private ListAdapter adapter;

    private static final double lat = 22.722990;
    private static final double lng = 75.857849;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_map);

        locationList=( ListView)findViewById(R.id.list_Surroundings);



        // Hashmap for ListView
        ArrayList<HashMap<String, String>> locationOfList = new ArrayList<HashMap<String, String>>();

        adapter = new SimpleAdapter(this, locationOfList,
                R.layout.list_item1,
                new String[] { TAG_NAME }, new int[] {
                        R.id.name});

        locationList.setAdapter(adapter);

     // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

     // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
         // Getting Array of Category
        location=json.getJSONArray(TAG_LOCATION);

        //looping through all categories
        for(int i = 0; i < location.length(); i++){
            JSONObject c = location.getJSONObject(i);
            JSONObject c1=c.getJSONObject(TAG_LOCATION1);

            // Storing each json item in variable
                String LocationID = c1.getString(TAG_LOCATION_ID);
                String Name = c1.getString(TAG_NAME);
                String Phone = c1.getString(TAG_PHONE);
                String FormattedPhone = c1.getString(TAG_FORMATTED_PHONE);
                String Address = c1.getString(TAG_ADDRESS);
                String CrossStreet = c1.getString(TAG_CROSS_STREET);
                String Lat = c1.getString(TAG_LAT);
                String Lng = c1.getString(TAG_LNG);
                String Distance = c1.getString(TAG_DISTANCE);
                String PostalCode = c1.getString(TAG_POSTAL_CODE);
                String City = c1.getString(TAG_CITY);
                String State = c1.getString(TAG_STATE);
                String Country = c1.getString(TAG_COUNTRY);

            // creating new HashMap
               HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
               map.put(TAG_LOCATION_ID, LocationID);
               map.put(TAG_NAME, Name);
               map.put(TAG_PHONE, Phone);
               map.put(TAG_FORMATTED_PHONE, FormattedPhone);
               map.put(TAG_ADDRESS, Address);
               map.put(TAG_CROSS_STREET, CrossStreet);
               map.put(TAG_LAT, Lat);
               map.put(TAG_LNG, Lng);
               map.put(TAG_DISTANCE, Distance); 
               map.put(TAG_POSTAL_CODE, PostalCode);
               map.put(TAG_CITY, City);
               map.put(TAG_STATE, State);
               map.put(TAG_COUNTRY, Country);

               // adding HashList to ArrayList
               locationOfList.add(map);
        }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

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

        List mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(
                R.drawable.map_pin_red);
        CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(
                drawable, this);

        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem = new OverlayItem(point, "Hello",
                "I'm in Athens, Greece!");

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

        mapController = mapView.getController();

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

        lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener= new MylocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    public class MylocationListener implements LocationListener{


        @Override
        public void onLocationChanged(Location loc) {
            // TODO Auto-generated method stub
            if(loc!=null){
                Toast.makeText(getBaseContext(), "Location Changed : Lat:" +loc.getLatitude() + "Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
            }
            GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            mapController.animateTo(point);
            mapController.setZoom(10);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

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

}
4

0 回答 0