2010-06-02 9 views
9

के भीतर निहित है वर्तमान में मैं एक सूची दृश्य में MapView लगाने की कोशिश कर रहा हूं। क्या किसी के साथ कोई सफलता मिली है? क्या यह भी संभव है? यहां मेरा कोड है:एंड्रॉइड - MapView एक सूचीदृश्य

  ListView myList = (ListView) findViewById(android.R.id.list); 
     List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>(); 

     Map<String, Object> curGroupMap = new HashMap<String, Object>(); 
     groupData.add(curGroupMap); 
     curGroupMap.put("ICON", R.drawable.back_icon); 
     curGroupMap.put("NAME","Go Back"); 
     curGroupMap.put("VALUE","By clicking here"); 

     Iterator it = data.entrySet().iterator(); 
     while (it.hasNext()) 
     { 
      //Get the key name and value for it 
      Map.Entry pair = (Map.Entry)it.next(); 
      String keyName = (String) pair.getKey(); 
      String value = pair.getValue().toString(); 

      if (value != null) 
      { 
       //Add the parents -- aka main categories 
       curGroupMap = new HashMap<String, Object>(); 
       groupData.add(curGroupMap); 

       //Push the correct Icon 
       if (keyName.equalsIgnoreCase("Phone")) 
        curGroupMap.put("ICON", R.drawable.phone_icon); 
       else if (keyName.equalsIgnoreCase("Housing")) 
        curGroupMap.put("ICON", R.drawable.house_icon); 
       else if (keyName.equalsIgnoreCase("Website")) 
        curGroupMap.put("ICON", R.drawable.web_icon); 
       else if (keyName.equalsIgnoreCase("Area Snapshot")) 
        curGroupMap.put("ICON", R.drawable.camera_icon); 
       else if (keyName.equalsIgnoreCase("Overview")) 
        curGroupMap.put("ICON", R.drawable.overview_icon); 
       else if (keyName.equalsIgnoreCase("Location")) 
        curGroupMap.put("ICON", R.drawable.map_icon); 
       else 
        curGroupMap.put("ICON", R.drawable.icon); 

       //Pop on the Name and Value 
       curGroupMap.put("NAME", keyName); 
       curGroupMap.put("VALUE", value); 
      } 
     } 

     curGroupMap = new HashMap<String, Object>(); 
     groupData.add(curGroupMap); 
     curGroupMap.put("ICON", R.drawable.back_icon); 
     curGroupMap.put("NAME","Go Back"); 
     curGroupMap.put("VALUE","By clicking here"); 

     //Set up adapter 
     mAdapter = new SimpleAdapter(
       mContext, 
       groupData, 
       R.layout.exp_list_parent, 
       new String[] { "ICON", "NAME", "VALUE" }, 
       new int[] { R.id.photoAlbumImg, R.id.rowText1, R.id.rowText2 } 
     ); 

     myList.setAdapter(mAdapter); //Bind the adapter to the list 

आपकी मदद के लिए अग्रिम धन्यवाद !!

+2

ठीक है, मैं काट देंगे। ListView में आपको MapView की आवश्यकता क्यों है? – CaseyB

+1

इसके अलावा, आप क्या समस्याएं अनुभव कर रहे हैं? – CommonsWare

+0

हाइपोटेटिकल - आप एक विशिष्ट व्यवसाय के लिए सबकुछ सूचीबद्ध करते हैं..मैं सूची में कहां पर एक मानचित्र प्रदर्शित करना चाहता हूं। मुझे जिस समस्या का सामना करना पड़ रहा है वह यह है कि मुझे नहीं पता कि कहां से शुरू करना है। मैं इसे एक संपूर्ण नए इरादे (जिस तरह से सूचीदृश्य उद्देश्य को हरा देता है) शुरू करने के बजाय इसे सूची दृश्य में शामिल करना चाहता हूं। – Ryan

उत्तर

5

उस स्थिति में आप सूची में मानचित्र दृश्य जोड़ देंगे जैसे कि आप कोई अन्य दृश्य देखेंगे। एक कस्टम सूची एडाप्टर बनाने के तरीके पर Here's a quick tutorial। लेकिन मुझे आपको सावधान करना होगा, एक नक्शा व्यू एक बहुत भारी दृश्य है और यदि आप स्क्रीन पर उनमें से एक समूह प्राप्त करने का प्रयास करते हैं तो आप ऐप को सुस्त होने पर ध्यान देंगे! आप सूची आइटम में केवल एक बटन जोड़ सकते हैं जो उपयोगकर्ता को किसी अन्य पृष्ठ पर ले जाता है जिसमें मानचित्र सहित अधिक जानकारी होती है।

+2

मुझे लगता है कि लिंक कस्टम एडाप्टर में मैपव्यू दिखाने के लिए नहीं है ... कृपया मुझे सूची एडाप्टर में मानचित्र रखने के लिए मार्गदर्शन करें –

+0

मेरे पास SupportMapFragment है, क्या मैं सूची सूची पंक्ति के अंदर उपयोग कर सकता हूं? मेरे पास कस्टम एडाप्टर है लेकिन मुझे 'BaseExpandableListAdapter' के' getChildView() 'के अंदर मानचित्र का उपयोग करने का तरीका नहीं मिल रहा है। बस हम '((TextView) convertView.findViewById (R.id.parent_txt_list_title)) की तरह कर सकते हैं। SetText (parent.getTitle()); लेकिन हम मानचित्र के लिए एक ही चीज़ कैसे कर सकते हैं। क्या आप कृपया मेरी मदद कर सकते हैं –

+0

मैंने एक सूचीदृश्य में एक नक्शा जोड़ा है लेकिन ज़ूम/चुटकी समस्या का सामना कर रहा है। पेरेंट स्क्रॉलिंग के कारण। –

6

सबसे पहले, मुझे नहीं लगता कि एक बार में कई MapViews प्रदर्शित करना काम करेगा। MapActivity दस्तावेज़ जो केवल एक प्रक्रिया प्रति समर्थित है:

"प्रति प्रक्रिया केवल एक मानचित्र सक्रियता समर्थित है। एकाधिक मानचित्र क्रियाएं एक साथ चल रही हैं, अप्रत्याशित और अवांछित तरीकों में हस्तक्षेप करने की संभावना है।"

(http://code.google.com/android/add-ons/google-apis/reference/index.html)

यह स्पष्ट कहना नहीं है कि आप एक MapActivity के भीतर कई MapViews नहीं हो सकता है लेकिन मुझे लगता है वे के रूप में अच्छी माता पिता किस तरह ViewGroup वे शामिल हों की परवाह किए बिना हस्तक्षेप करेंगे।

दूसरे, आप स्थिर नक्शे एपीआई का उपयोग कर ListView में शामिल करने के लिए एक सरल छवि प्राप्त करने पर विचार हो सकता है - एक पूरी तरह से विकसित MapView हो सकता है अनावश्यक रूप से किसी भी मामले में दिग्गज:

http://code.google.com/apis/maps/documentation/staticmaps/

एक समस्या जिसका आप संभावित रूप से सामना कर सकते हैं वह यह है कि स्टेटिक मैप्स एपीआई "उपयोगकर्ता" द्वारा उपयोग को सीमित करता है, जिसका शायद आईपी (इसे एपीआई कुंजी की आवश्यकता नहीं है) का मतलब है, और मोबाइल नेटवर्क आईपी उपयोग सीमित होने के साथ समस्याग्रस्त हो सकते हैं। मुझे यकीन नहीं है कि यह कैसे खेलेंगे।

+0

अरे स्टीव - मैं एकाधिक मानचित्रदृश्य प्रदर्शित करने की कोशिश नहीं कर रहा हूं ... केवल एक। इसके अलावा लक्ष्य यह है कि उपयोगकर्ता मानचित्र के साथ बातचीत करे ताकि एक स्थिर नक्शा दुर्भाग्य से काम नहीं करेगा। हालांकि आपके सुझाव के लिए धन्यवाद :) – Ryan

+0

आह, मैं देखता हूं। उस स्थिति में, यदि आप अपनी सूची में आइटम बड़े पैमाने पर विषम हैं, तो आप या तो कस्टम सूची एडाप्टर का उपयोग कर सकते हैं, जैसा कि केसीबी ने नोट किया है, या शायद स्क्रॉलव्यू के भीतर एक वर्टिकल लीनियरलाउट का उपयोग करें। – Steve

53

एक पुराने पुराने जवाब (वास्तव में 2 साल से अधिक) के लिए वैकल्पिक समाधान पोस्ट करने के लिए, लेकिन मैंने सोचा कि यह किसी ऐसे व्यक्ति की मदद कर सकता है जो इस पोस्ट पर ठोकर खा सकता है जैसा मैंने किया था।

नोट: यह किसी ऐसे व्यक्ति के लिए उपयोगी हो सकता है जो आसानी से "मानचित्र" में स्थान प्रदर्शित करने की आवश्यकता है लेकिन ListView में इसके साथ बातचीत करने की आवश्यकता नहीं है। वास्तविक मानचित्र, कहते हैं पर प्रदर्शित किया जा सकता एक विवरण पेज, ListView

के रूप में किसी आइटम पर क्लिक करने के बाद पहले से ही @CaseyB से कहा, MapView एक भारी देखने की तरह है। उस पहलू का मुकाबला करने के लिए (और मेरे लिए जीवन को थोड़ा आसान बनाना ;-)), मैंने अपने आवेदन के लिए आवश्यक कई पैरामीटर का उपयोग करके एक स्थिर Google मानचित्र के लिए एक यूआरएल बनाना चुना है। आप और अधिक विकल्प यहाँ प्राप्त कर सकते: https://developers.google.com/maps/documentation/staticmaps/

सबसे पहले, जब मैं अपने ListView, मैं इस तरह के अक्षांश और देशांतर एक स्ट्रिंग के लिए कुछ स्थिर लिंक ऊपर उल्लेख से लिया चर के साथ के रूप में डेटा पास के लिए डेटा का निर्माण कर रहा हूँ । मुझे फेसबुक एपीआई से मेरे समन्वय मिलते हैं।

कोड मैं लिंक के निर्माण के लिए उपयोग करें:

String getMapURL = "http://maps.googleapis.com/maps/api/staticmap?zoom=18&size=560x240&markers=size:mid|color:red|" 
+ JOLocation.getString("latitude") 
+ "," 
+ JOLocation.getString("longitude") 
+ "&sensor=false"; 

ऊपर का निर्माण किया यूआरएल, जब एक ब्राउज़र में इस्तेमाल किया, एक .PNG फ़ाइल देता है। फिर, गतिविधि के लिए मेरे adapter में, मैं कस्टम ListView में प्रदर्शित करने के लिए पहले बनाए गए यूआरएल से उत्पन्न छवि को प्रदर्शित करने के लिए @ Fedor's आलसी लोडिंग का उपयोग करता हूं। आप इस Map (वास्तव में मानचित्र की छवि) को प्रदर्शित करने के लिए अपना स्वयं का तरीका चुन सकते हैं।

अंतिम परिणाम का एक उदाहरण।

enter image description here

वर्तमान में, मैं के बारे में 30 अजीब चेकइन नक्शे इस ListView में (मैं फेसबुक SDK के साथ इसका इस्तेमाल), लेकिन उन उनमें से 100 की हो सकती है और इसके बारे में पूरी तरह से कोई रिपोर्ट धीमा वहाँ किया गया है नीचे।

मुझे संदेह है कि यह प्रश्न के बाद पारित समय पर विचार करने में ओपी की मदद नहीं कर सकता है, लेकिन उम्मीद है कि यह भविष्य में इस पृष्ठ पर अन्य उपयोगकर्ताओं को लैंडिंग में मदद करेगा।

+1

उत्कृष्ट काम! – ChaturaM

+1

वाह, शानदार उत्तर! – tskulbru

+2

बेहतर तरीके से नहीं सोच सका। अच्छा काम। – Anirudh

-1

मैं एक ही समस्या का सामना करना पड़ा आज - आप अपने MapActivity अंदर MapView बनाने के लिए अन्यथा आप देखें com.google.maps.MapView या तो बढ़ाने के लिए असमर्थ की तरह एक त्रुटि मिल जाएगा है कि पता चला है .. इस MapView को अपनी सूची एडाप्टर में पास करने से पहले और आवश्यकता होने पर इसे थूक दें। ऊंचाई और चौड़ाई को समायोजित करने के लिए मुझे सापेक्ष लयआउट के अंदर मैप व्यू को रखना था (किसी कारण से MapView "सामान्य" व्यवहार नहीं करता है)। तुम मुझे विवरण के लिए पूछ सकते अगर आप :)

1

यह संभव है की तरह है, GoogleMapSample कोड से ही:

/** 
* This shows to include a map in lite mode in a ListView. 
* Note the use of the view holder pattern with the 
* {@link com.google.android.gms.maps.OnMapReadyCallback}. 
*/ 
public class LiteListDemoActivity extends AppCompatActivity { 

    private ListFragment mList; 

    private MapAdapter mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.lite_list_demo); 

     // Set a custom list adapter for a list of locations 
     mAdapter = new MapAdapter(this, LIST_LOCATIONS); 
     mList = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list); 
     mList.setListAdapter(mAdapter); 

     // Set a RecyclerListener to clean up MapView from ListView 
     AbsListView lv = mList.getListView(); 
     lv.setRecyclerListener(mRecycleListener); 

    } 

    /** 
    * Adapter that displays a title and {@link com.google.android.gms.maps.MapView} for each item. 
    * The layout is defined in <code>lite_list_demo_row.xml</code>. It contains a MapView 
    * that is programatically initialised in 
    * {@link #getView(int, android.view.View, android.view.ViewGroup)} 
    */ 
    private class MapAdapter extends ArrayAdapter<NamedLocation> { 

     private final HashSet<MapView> mMaps = new HashSet<MapView>(); 

     public MapAdapter(Context context, NamedLocation[] locations) { 
      super(context, R.layout.lite_list_demo_row, R.id.lite_listrow_text, locations); 
     } 


     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      ViewHolder holder; 

      // Check if a view can be reused, otherwise inflate a layout and set up the view holder 
      if (row == null) { 
       // Inflate view from layout file 
       row = getLayoutInflater().inflate(R.layout.lite_list_demo_row, null); 

       // Set up holder and assign it to the View 
       holder = new ViewHolder(); 
       holder.mapView = (MapView) row.findViewById(R.id.lite_listrow_map); 
       holder.title = (TextView) row.findViewById(R.id.lite_listrow_text); 
       // Set holder as tag for row for more efficient access. 
       row.setTag(holder); 

       // Initialise the MapView 
       holder.initializeMapView(); 

       // Keep track of MapView 
       mMaps.add(holder.mapView); 
      } else { 
       // View has already been initialised, get its holder 
       holder = (ViewHolder) row.getTag(); 
      } 

      // Get the NamedLocation for this item and attach it to the MapView 
      NamedLocation item = getItem(position); 
      holder.mapView.setTag(item); 

      // Ensure the map has been initialised by the on map ready callback in ViewHolder. 
      // If it is not ready yet, it will be initialised with the NamedLocation set as its tag 
      // when the callback is received. 
      if (holder.map != null) { 
       // The map is already ready to be used 
       setMapLocation(holder.map, item); 
      } 

      // Set the text label for this item 
      holder.title.setText(item.name); 

      return row; 
     } 

     /** 
     * Retuns the set of all initialised {@link MapView} objects. 
     * 
     * @return All MapViews that have been initialised programmatically by this adapter 
     */ 
     public HashSet<MapView> getMaps() { 
      return mMaps; 
     } 
    } 

    /** 
    * Displays a {@link LiteListDemoActivity.NamedLocation} on a 
    * {@link com.google.android.gms.maps.GoogleMap}. 
    * Adds a marker and centers the camera on the NamedLocation with the normal map type. 
    */ 
    private static void setMapLocation(GoogleMap map, NamedLocation data) { 
     // Add a marker for this item and set the camera 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(data.location, 13f)); 
     map.addMarker(new MarkerOptions().position(data.location)); 

     // Set the map type back to normal. 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    } 

    /** 
    * Holder for Views used in the {@link LiteListDemoActivity.MapAdapter}. 
    * Once the the <code>map</code> field is set, otherwise it is null. 
    * When the {@link #onMapReady(com.google.android.gms.maps.GoogleMap)} callback is received and 
    * the {@link com.google.android.gms.maps.GoogleMap} is ready, it stored in the {@link #map} 
    * field. The map is then initialised with the NamedLocation that is stored as the tag of the 
    * MapView. This ensures that the map is initialised with the latest data that it should 
    * display. 
    */ 
    class ViewHolder implements OnMapReadyCallback { 

     MapView mapView; 

     TextView title; 

     GoogleMap map; 

     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      MapsInitializer.initialize(getApplicationContext()); 
      map = googleMap; 
      NamedLocation data = (NamedLocation) mapView.getTag(); 
      if (data != null) { 
       setMapLocation(map, data); 
      } 
     } 

     /** 
     * Initialises the MapView by calling its lifecycle methods. 
     */ 
     public void initializeMapView() { 
      if (mapView != null) { 
       // Initialise the MapView 
       mapView.onCreate(null); 
       // Set the map ready callback to receive the GoogleMap object 
       mapView.getMapAsync(this); 
      } 
     } 

    } 

    /** 
    * RecycleListener that completely clears the {@link com.google.android.gms.maps.GoogleMap} 
    * attached to a row in the ListView. 
    * Sets the map type to {@link com.google.android.gms.maps.GoogleMap#MAP_TYPE_NONE} and clears 
    * the map. 
    */ 
    private AbsListView.RecyclerListener mRecycleListener = new AbsListView.RecyclerListener() { 

     @Override 
     public void onMovedToScrapHeap(View view) { 
      ViewHolder holder = (ViewHolder) view.getTag(); 
      if (holder != null && holder.map != null) { 
       // Clear the map and free up resources by changing the map type to none 
       holder.map.clear(); 
       holder.map.setMapType(GoogleMap.MAP_TYPE_NONE); 
      } 

     } 
    }; 

    /** 
    * Location represented by a position ({@link com.google.android.gms.maps.model.LatLng} and a 
    * name ({@link java.lang.String}). 
    */ 
    private static class NamedLocation { 

     public final String name; 

     public final LatLng location; 

     NamedLocation(String name, LatLng location) { 
      this.name = name; 
      this.location = location; 
     } 
    } 
} 

पूर्ण कोड उपलब्ध है: https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/LiteListDemoActivity.java

+0

लिंक मृत अद्यतन लिंक है https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/java/com/example/mapdemo/LiteListDemoActivity.java – Killer