位置根据列表显示在地图上?

时间:2013-04-10 09:34:23

标签: android google-maps listview android-activity android-mapview

我正在尝试将Google MapviewListView合并为一个activityMapview取上边的2/3,下边的1/3(纵向)。

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

MapViewListView两者都会收到相同的JSON信息,他们会获得onCreate()。我已经能够在屏幕的MapView上放置2/3,并获取其获取位置的ListView数据。 但我希望我listView上的位置也显示在map上,并设置了针脚,有人可以告诉我,如何显示location map

这是我的代码:

// url发出请求     private static String 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;
    }

}

0 个答案:

没有答案