【问题标题】:How to get multiple data from ArrayList and display it in snippet google map android?如何从 ArrayList 获取多个数据并将其显示在片段谷歌地图 android 中?
【发布时间】:2016-01-07 20:51:14
【问题描述】:

我正在做一个谷歌地图项目,我必须从数组列表(从服务器检索)中获取数据(标题和 sn-p 描述)并动态显示在标题和 sn-p 中。由于 sn-p 中的描述很长,所以不会显示整个描述。以下是我的代码。

当我点击标记时,我可以获得一个标题和一个 sn-p。我需要的是,sn-p 应该显示来自服务器的冗长描述。目前发生的情况是,有一个标题行和一个 sn-p 行。描述在 sn-p 中显示了一半。如果我不清楚,请告诉我。需要解决这个问题。

@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

    }


}

目前,当我点击标记时,我可以获得一个标题和一个 sn-p。我需要的是,sn-p 应该显示来自服务器的冗长描述。目前发生的情况是,有一个标题行和一个 sn-p 行。描述在 sn-p 中显示了一半。如果我不清楚,请告诉我。需要解决这个问题。

【问题讨论】:

  • 您的问题是什么?您在哪个部分遇到了问题?
  • @Yash 你能展示你的自定义信息窗口布局吗?
  • 没有。这就是问题
  • 信息窗口有现成的代码吗?我可以在哪里写描述 4-5 行?
  • @Yash 检查我更新的答案。我引用了一个类似的问题来指导您使用自定义信息窗口

标签: android google-maps


【解决方案1】:

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

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

【讨论】:

  • 你检查过链接吗?
  • @Yash 是的,我知道。您的代码不正确,这就是为什么我向您指出如何创建自定义信息窗口并为您提供一些有关如何满足要求的提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 2018-05-16
  • 2018-09-15
  • 2015-02-07
相关资源
最近更新 更多