如何从ArrayList获取多个数据并将其显示在片段谷歌地图android?

时间:2016-01-07 06:20:54

标签: android google-maps

我正在进行谷歌地图项目,我必须从数组列表中获取数据(标题和摘录说明)(从服务器检索)并动态显示在标题和代码段中。由于片段中的描述很长,因此不显示整个描述。以下是我的代码。

点按标记后,我可以获得标题和摘要。我需要的是,该代码片段应该显示来自服务器的冗长描述。目前发生的是,有一个标题行和一个片段行。描述显示在代码段的一半。如果我不清楚请告诉我。需要解决这个问题。

@SuppressLint("NewApi")
public class GoogleActivity extends FragmentActivity implements LocationListener {

    private LocationManager locationManager;
    private static final long MIN_TIME = 700;
    private static final float MIN_DISTANCE = 800;

    private Location mLocation;

    // Google Map
    private GoogleMap googleMap;
    LatLng myPosition;

    // All static variables
    static final String URL = "http://webersspot.accountsupport.com/gmaptrial/onedb/phpsqlajax_genxml.php";
    // XML node keys

    static final String KEY_PID = "pro"; // parent node
    static final String KEY_NAME = "Name";
    static final String KEY_DESCRIPTION = "Description";
    static final String KEY_LAT = "Latitude";
    static final String KEY_LONG = "Longitude";

    ArrayList<HashMap<String, String>> storeMapData = new ArrayList<HashMap<String, String>>();
    private ShareActionProvider mShareActionProvider;

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


        //open the map
        openTheMap();


        /*

     // Get Location Manager and check for GPS & Network location services
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
              !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
          // Build the alert dialog
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle("Location Services Not Active");
          builder.setMessage("Please enable Location Services and GPS");
          builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialogInterface, int i) {
            // Show location settings when the user acknowledges the alert dialog
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
            }
          });
          Dialog alertDialog = builder.create();
          alertDialog.setCanceledOnTouchOutside(false);
          alertDialog.show();
        }
         */


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); //You can also use LocationManager.GPS_PROVIDER and LocationManager.PASSIVE_PROVIDER        


        new LongOperation().execute("");
        new MapOperation().execute(googleMap);


    }



    /* open the map */
    private void openTheMap() {
        try {
            if(googleMap == null) {

                SupportMapFragment mapFragment =
                        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                googleMap = mapFragment.getMap();
                googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);  // Hybrid for satellite with place name
                googleMap.setMyLocationEnabled(true);  // enable user location button.
                googleMap.setInfoWindowAdapter(null) ;
                googleMap.getUiSettings().setZoomControlsEnabled(true);
                googleMap.getUiSettings().setCompassEnabled(true);
                googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                googleMap.getUiSettings().setAllGesturesEnabled(true);
                googleMap.setTrafficEnabled(true); // enable road 
                zoomMap();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* zoom current location */
    private void zoomMap() {
        int zoomScale = 12;
        double currentLat = mLocation.getLatitude();
        double currentLon = mLocation.getLongitude();
        googleMap.moveCamera(CameraUpdateFactory
                .newLatLngZoom(new LatLng(currentLat, currentLon), zoomScale));



    }

    public List<HashMap<String, String>> prepareData(){

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
        //List<HashMap<String, String>>  menuItems = new ArrayList<HashMap<String, String>>();

        XmlParser parser = new XmlParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_PID);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            System.out.println("OOOOOOOOOOOOOOOOOOO  ::: "+e.getAttribute(KEY_NAME));
            // adding each child node to HashMap key => value

            map.put(KEY_NAME, e.getAttribute(KEY_NAME).toString());
            map.put(KEY_DESCRIPTION ,e.getAttribute(KEY_DESCRIPTION).toString());
            map.put(KEY_LAT, e.getAttribute(KEY_LAT).toString());
            map.put(KEY_LONG ,e.getAttribute(KEY_LONG).toString());


            // adding HashList to ArrayList
            menuItems.add(map);
            storeMapData = menuItems;



        }
        return menuItems;

    }

    public void onMapReady(final GoogleMap map) {       
        ArrayList<HashMap<String, String>> processData = storeMapData;



        System.out.println( "kjkasdc   "+processData);

        for (int i=0; i< processData.size(); i++){


            final double lat = Double.parseDouble(processData.get(i).get(KEY_LAT));
            System.out.println("MAP LAT :::::::::::::::::::::::::  "+lat);
            final double lon =  Double.parseDouble(processData.get(i).get(KEY_LONG));
            System.out.println("MAP LON :::::::::::::::::::::::::  "+lon);
            final String address = processData.get(i).get(KEY_DESCRIPTION);
            System.out.println("MAP ADDRESS :::::::::::::::::::::::::  "+address);
            final String name = processData.get(i).get(KEY_NAME);
            System.out.println("MAP ADDRESS :::::::::::::::::::::::::  "+name);




            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title(name).snippet(address).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));



                }
            });

        }
    }



    @SuppressLint("NewApi")
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        /** Inflating the current activity's menu with res/menu/items.xml */
        getMenuInflater().inflate(R.menu.share_menu, menu);     

        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();

        /** Setting a share intent */
        mShareActionProvider.setShareIntent(getDefaultShareIntent());


        return super.onCreateOptionsMenu(menu);

    }    

    /** Returns a share intent */
    private Intent getDefaultShareIntent(){     
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");       
        intent.putExtra(Intent.EXTRA_SUBJECT,"Download");
        intent.putExtra(Intent.EXTRA_TEXT,"Download Hill Top Beauty Parlour App - Maroli from Google Play Store:  https://play.google.com/store/apps/details?id=beauty.parlour.maroli");        
        return intent;
    }


    private class LongOperation extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {         
            prepareData();      
            return "Executed";
        }
        @Override
        protected void onPostExecute(String result) {               
            System.out.println("Executed");
        }
        @Override
        protected void onPreExecute() {         
            System.out.println("Execution started");            
        }
        @Override
        protected void onProgressUpdate(Void... values) {

            System.out.println("     -- -- -- "+values);
        }
    }

    private class MapOperation extends AsyncTask<GoogleMap, Void, String> {
        @Override
        protected String doInBackground(GoogleMap... params) {    
            GoogleMap map = params[0];
            onMapReady(map);    
            return "Executed";
        }
        @Override
        protected void onPostExecute(String result) {               
            System.out.println(result);
        }
        @Override
        protected void onPreExecute() {         
            System.out.println("Execution started");            
        }
        @Override
        protected void onProgressUpdate(Void... values) {

            System.out.println("     -- -- -- "+values);
        }
    }

    class MyInfoWindowAdapter implements InfoWindowAdapter{

        private final View myContentsView;

        MyInfoWindowAdapter(){
            myContentsView = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
        }

        @Override
        public View getInfoContents(Marker marker) {

            TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
            tvTitle.setText(marker.getTitle());


            TextView tvaddress = ((TextView)myContentsView.findViewById(R.id.snippet));
            tvaddress.setText(marker.getTitle());




            return myContentsView;
        }



        @Override
        public View getInfoWindow(Marker marker) {
            // TODO Auto-generated method stub


            return null;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
        googleMap.animateCamera(cameraUpdate);
        locationManager.removeUpdates(this);

    }


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

    }


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

    }


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

    }


}

目前,当我点按标记时,我可以获得标题和摘要。我需要的是,该代码片段应该显示来自服务器的冗长描述。目前发生的是,有一个标题行和一个片段行。描述显示在代码段的一半。如果我不清楚请告诉我。需要解决这个问题。

1 个答案:

答案 0 :(得分:0)

您需要检查自定义信息窗口的布局,并确保描述的Textview接受超过1行。在布局或代码中设置lines或maxLines属性将帮助您实现此目的。还要确保正确设置了layout_height和layout_width。

你可以参考这个问题 - &gt; Custom info window for google maps android作为如何创建自定义信息窗口的指南