ऑस्मर्डोइड के लिए MarkerInfoWindow लिखना

यह लेख उन लोगों के लिए है जिनके पास ऑस्मोड्रोइड के साथ जानकारी जोड़ने में कठिनाई है और AsyncTask के साथ काम करना है, और बस उन लोगों के लिए जिन्होंने पहले ऐसा कुछ नहीं किया है। यहां मैंने लिखा कि कैसे मैंने परिवहन निगरानी सेवा में कार के बारे में डेटा प्राप्त करने के लिए एक विंडो बनाई।
लब्बोलुआब यह है कि जब आप मार्कर पर क्लिक करते हैं, तो कुछ डेटा ऑब्जेक्ट से लिया जाता है, अन्य डेटा एपीआई से लोड होता है, एप्लिकेशन डेटाबेस को लिखा जाता है, और फिर इन्फोविन्डो ऑब्जेक्ट में प्रदर्शित होता है।

हम जानकारी के विवरण के साथ एक xml फ़ाइल बनाकर शुरू करते हैं, परिवहन मानदंड के हेडर के साथ फ़ील्ड जोड़ते हैं।

छवि
MarkerInfoWindow आपको शीर्षक, विवरण, सबस्क्रिप्ट में रिकॉर्ड करने की अनुमति देता है, मैंने सभी आवश्यक रिकॉर्ड के लिए केवल डेस्क्रिप्टेन का उपयोग किया। हम बाद में हाइफ़न के साथ एक स्ट्रिंग के रूप में डेटा स्थानांतरित करेंगे।

मार्कअप निम्नानुसार है:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/balloon_overlay_white" > <ImageView android:id="@+id/bubble_image" android:layout_width="65dp" android:layout_height="65dp" android:visibility="gone" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:orientation="vertical" > <TextView android:id="@+id/bubble_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:maxEms="17" android:layout_gravity="left" android:layout_weight="1" android:text="Title" android:textSize="18dp" android:textIsSelectable="false" /> <Button android:id="@+id/bubble_moreinfo" android:background="@drawable/btn_moreinfo" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="0" /> <TextView android:id="@+id/bubble_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dp" android:maxEms="17" android:text="Description" android:layout_toRightOf="@+id/model" android:layout_below="@+id/bubble_title" android:lineSpacingExtra="3dp" android:layout_marginLeft="3dp" /> <TextView android:id="@+id/model" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff6c6c6c" android:textSize="14dp" android:maxEms="17" android:text="@string/model" android:layout_below="@+id/bubble_title" android:layout_alignRight="@+id/organization" /> <TextView android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff6c6c6c" android:textSize="14dp" android:maxEms="17" android:text="@string/group" android:layout_below="@+id/model" android:layout_alignRight="@+id/organization" /> <TextView android:id="@+id/organization" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff6c6c6c" android:textSize="14dp" android:maxEms="17" android:text="@string/organization" android:layout_below="@+id/group" /> <TextView android:id="@+id/speed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff6c6c6c" android:textSize="14dp" android:maxEms="17" android:text="@string/speed" android:layout_below="@+id/organization" android:layout_alignRight="@+id/organization" /> <TextView android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff6c6c6c" android:textSize="14dp" android:maxEms="17" android:text="@string/update" android:layout_below="@+id/speed" android:layout_alignRight="@+id/organization" /> <TextView android:id="@+id/bubble_subdescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="10dp" android:maxEms="17" android:text="Address" android:layout_below="@+id/update" /> </RelativeLayout> </LinearLayout> 



अगला, आवश्यक डेटा प्रदर्शित करने के लिए, हम अपना CarInfoWindow क्लास लिखते हैं:
 public class CarInfoWindow extends MarkerInfoWindow { Car mCar; Marker mMarker; CarInfoTask carInfoTask; Boolean stopped = false; Drawable icon; String active; public CarInfoWindow(int layoutResId, MapView mapView) { super(layoutResId, mapView); } //   asyncTask @Override public void onOpen(final Object item) { if (!stopped) { carInfoTask = new CarInfoTask(); carInfoTask.execute(item); } super.onOpen(item); } //      @Override public void onClose() { stopped = false; super.onClose(); if (!(mCar.getLastUpdate() == null)) { if (((System.currentTimeMillis() - Long.parseLong(mCar.getLastUpdate())) / 3600000) > 1) { active = "0"; //car is not active } else { active = "1"; } } String fileName = Environment.getExternalStorageDirectory()+"/automap/cars/icons/"+mCar.getIconIndex()+"/"+active+"/icon.png"; icon = Utils.loadIcon(fileName, Float.parseFloat(mCar.getDirection()), mCar.getIconType()); mMarker.setIcon(icon); } class CarInfoTask extends AsyncTask<Object, String, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override //        protected Void doInBackground(Object... params) { try { //  ,     Car    mMarker = (Marker)params[0]; mCar = (Car) mMarker.getRelatedObject(); //,     infoWindow String markName; String carModelName; String groupName; String carLastUpdate; Context context = getView().getContext(); // token,     Token token = Prefs.getToken(getView().getContext()); String markId = mCar.getMarkId(); String modelId = mCar.getModelId(); String orgId = mCar.getOrganizationId(); String groupId = mCar.getGroupId(); carLastUpdate = mCar.getLastUpdate(); //-  String formattedDate = ""; if (!(carLastUpdate == null)) { long unixSeconds = Long.parseLong(carLastUpdate); Date date = new Date(unixSeconds); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss "); formattedDate = sdf.format(date); } String organizationName = DbHelper.getInstance(context).getOrganizationStatById(orgId).getName(); List<Mark> marksList = null; List<CarModel> carModelsList = null; List<Group> groupsList = null; //   API,  mark  model  id try { // retrofit carModelsList = Api.getService(context).getModels(token.getValue(), markId); int size = carModelsList.size(); for (int i=0; i<size; i++) { // cupboard      DbHelper.getInstance(context).insertCarModels(carModelsList.get(i)); } groupsList = Api.getService(context).getGroups(token.getValue(), orgId); size = groupsList.size(); for (int i=0; i<size; i++) { DbHelper.getInstance(context).insertGroups(groupsList.get(i)); } } catch (Exception e) { e.printStackTrace(); } markName = DbHelper.getInstance(context).getMarkById(markId).getName(); carModelName = DbHelper.getInstance(context).getCarModelById(modelId).getName(); groupName = DbHelper.getInstance(context).getGroupById(groupId).getName(); //       UI. publishProgress(organizationName, markName, carModelName, groupName, formattedDate); //values[0] = organizationName, values[1] = markName  .. } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); //   textView,     publishProgress() mMarker.hideInfoWindow(); mMarker.setTitle(mCar.getCarNo() + " (" + values[1] + ")"); mMarker.setSnippet( values[2] + "\n" + values[3] + "\n" + values[0] + "\n" + mCar.getSpeed() + "\n" + values[4] ); mMarker.setSubDescription(""); //    String fileName = Environment.getExternalStorageDirectory()+"/automap/cars/icons/"+mCar.getIconIndex()+"/2/icon.png"; icon = Utils.loadIcon(fileName, Float.parseFloat(mCar.getDirection()), mCar.getIconType()); mMarker.setIcon(icon); } //,     asyncTask @Override protected void onPostExecute(Void result) { stopped = true; mMarker.showInfoWindow(); super.onPostExecute(result); } } } 



अब, एक मार्कर बनाते समय, यह केवल वांछित विंडो को मार्कर से बांधने के लिए रहता है

 marker.setInfoWindow(infoWindow); 


जोड़तोड़ के परिणामस्वरूप हमें कुछ मिलता है:

छवि

PS इस तथ्य के बावजूद कि मार्कर ट्रक का आइकन है, और विवरण में यह अल्फा रोमियो है, सब कुछ सही ढंग से काम करता है।

Source: https://habr.com/ru/post/In224289/


All Articles