【问题标题】:Add an image for Android marker info window为 Android 标记信息窗口添加图像
【发布时间】:2016-07-25 05:06:12
【问题描述】:

我正在制作一个安卓应用程序。在这个应用程序中,有一些酒店的标记,当用户点击标记信息窗口时,它应该如下图所示。 (这是一个网络应用程序)

我的导航抽屉片段中有一个列表视图,而地图片段有标记。

这是我在尝试为MainActivity.java 中的信息窗口加载图像之前的代码。

  // Load Hotels
    private class GetHotelTask extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... data) {
            String identifier = data[0];

            // Toast.makeText(getContext(), "Manuli "+identifier, Toast.LENGTH_LONG ).show();
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://api.ayubo.lk/android/retrievehotels");
            HttpResponse response = null;
            String responseStr = null;

            try {
                //add data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("long", data[0]));
                nameValuePairs.add(new BasicNameValuePair("lat", data[1]));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                //execute http post
                response = httpclient.execute(httppost);
                responseStr = EntityUtils.toString(response.getEntity());

            } catch (ClientProtocolException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return responseStr;
        }

        protected void onPostExecute(String result) {

            try {
                JSONArray jObj = new JSONArray(result);

                for(int a=0;a<jObj.length();a++){

                JSONArray obj= (JSONArray) jObj.get(a);

                    Log.i("json", obj.get(3).toString());
                    Marker marker = GoogleMapsFragment.map.addMarker(new MarkerOptions()
                            .position(new LatLng( Double.valueOf(obj.get(3).toString()),  Double.valueOf(obj.get(2).toString())))
                            .title(obj.get(1).toString()+" | simple Price : "+obj.get(4).toString())
                            .snippet(obj.get(5).toString())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_hotel)));


                    hotelMarkers.add(marker);

                }

                LatLng latLng = new LatLng(Double.valueOf(latitude),Double.valueOf(longtitude));

                GoogleMapsFragment.map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                GoogleMapsFragment.map.animateCamera(CameraUpdateFactory.zoomTo(15));

            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

        }

    }

这是MainActivity.java内酒店的listview调用。

// Hotels
            case 0:
                if (A == 0){
                    Bundle bundle = new Bundle();
                    bundle.putString("restaurants", "Default");
                    fragmentObj = new GoogleMapsFragment();
                    fragmentObj.setArguments(bundle);

                    if (!sclItems.contains(0)){
                        new GetHotelTask().execute(longtitude, latitude);
                        sclItems.add(0);
                    }else {
                        removeHotels();
                        sclItems.remove((Integer)0);
                    }
                    A = 1;
                }else {
                    if (!sclItems.contains(0)){
                        new GetHotelTask().execute(longtitude, latitude);
                        sclItems.add(0);
                    }else {
                        removeHotels();
                        sclItems.remove((Integer)0);
                    }
                }
                break;

我尝试添加info_window_layout.xml,这是代码。

<?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">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_prof"/>
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12dp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"/>
    </LinearLayout>

</LinearLayout>

但是,我无法像图片中那样加载我的信息窗口。我尝试了几种解决方案,包括this,但其中任何一个都没有给我解决方案。

有人可以帮我解决这个问题吗?

提前致谢。 :)

  • 错误 -

【问题讨论】:

    标签: android image google-maps google-maps-markers infowindow


    【解决方案1】:

    终于想通了。 :D

    像这样更改了info_window_layout.xml

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000" >
        <LinearLayout
            android:id="@+id/text_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/markerImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               />
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/distance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>
    

    然后添加了一个名为ManiInfoAdapter.java的新类

    class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    
        private LayoutInflater inflater;
        private Context context;
    
        public MapInfoWindowAdapter(Context context){
            inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            this.context = context;
        }
    
        @Override
        public View getInfoWindow(Marker marker) {
            // Getting view from the layout file
            View v = inflater.inflate(R.layout.info_window_layout, null);
    
            TextView title = (TextView) v.findViewById(R.id.title);
            title.setText(marker.getTitle());
    
            TextView address = (TextView) v.findViewById(R.id.distance);
            address.setText(marker.getSnippet());
    
            ImageView iv = (ImageView) v.findViewById(R.id.markerImage);
            iv.setImageResource(R.drawable.ic_attraction1);
    
            return v;
        }
    
        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    }
    

    现在按预期工作。 :)

    【讨论】:

      【解决方案2】:

      试试这个代码:

         Marker kiel=FragmentLeft.map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(p.getLatitude()), Double.parseDouble(p.getLongitude())))
                      .title(p.getEventName()).snippet(getLocationStringAddress(new LatLng(Double.parseDouble(p.getLatitude()), Double.parseDouble(p.getLongitude()))))
                      .icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_set_marker)));
              markers.put(kiel.getId(),"http://"+strPhoto);
              kiel.setInfoWindowAnchor(1.95f,0.0f);
              hMarker.put(kiel,p);
              map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                  @Override
                  public View getInfoWindow(Marker arg0) {
                      View myContentView = flipscreen.this.getLayoutInflater().inflate(R.layout.custommarker, null);
                      TextView tvTitle = ((TextView) myContentView.findViewById(R.id.title));
                      tvTitle.setText(arg0.getTitle());
                      TextView tvSnippet = ((TextView) myContentView.findViewById(R.id.snippet));
                      tvSnippet.setText(arg0.getSnippet());
                      ImageView imgMarkerImage=(ImageView)myContentView.findViewById(R.id.imgMarkerImage);
                      System.out.println("List of Event Photos = " + strPhoto);
                      Picasso.with(flipscreen.this).load(markers.get(arg0.getId())).into(imgMarkerImage);
                      return myContentView;
                  }
                  @Override
                  public View getInfoContents(Marker marker) {
                      return null;
                  }
              });
              map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                  @Override
                  public void onInfoWindowClick(Marker marker) {
                      Iterator myVeryOwnIterator = hMarker.keySet().iterator();
                      while(myVeryOwnIterator.hasNext()) {
                          Marker key=(Marker)myVeryOwnIterator.next();
                          if(marker.getId().equals(key.getId())){
                              Event value=(Event)hMarker.get(key);
                              Activity_Event_Details.setEventDetails(value);
                              startActivity(new Intent(flipscreen.this,Activity_Event_Details.class));
      
                          }
                      }
                  }
              });
      

      【讨论】:

        【解决方案3】:

        我尝试为我的应用做同样的事情。我无法将图像加载到我想要的标记中。

        所以我做的是。

        1. 创建了一个xml布局maps_info_window_blue

        2. 我包含在我的地图布局中。

          <fragment
              android:id="@+id/map_booking_frg"
              android:name="com.google.android.gms.maps.SupportMapFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
          
          
          <ImageView
              android:id="@+id/center_marker"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@id/shadowView"
              android:layout_centerHorizontal="true"
              android:src="@drawable/marker_start" />
          
          <include layout="@layout/maps_info_window_blue"
              android:id="@+id/rl_info_window_blue"
              android:layout_width="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_above="@+id/shadowView"
              android:layout_height="wrap_content"/>
          
          <View
              android:id="@+id/shadowView"
              android:layout_width="wrap_content"
              android:layout_height="1dp"
              android:background="@color/transparent"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:visibility="invisible" />
          

        对我来说,要求是将标记信息窗口放在中心。您可以随意设置它。

        1. 为了给人一种标记的错觉,我总是将 LatLng 设置为我想要的地图中心(CameraPosition 目标设置为我想要的 LatLng)

        【讨论】:

          【解决方案4】:

          尝试使用信息窗口适配器。我想这可能会对你有所帮助。

          mMap.setInfoWindowAdapter(setMarkerWindow());
          
          
          
          
              private GoogleMap.InfoWindowAdapter setMarkerWindow() {
                          return new GoogleMap.InfoWindowAdapter() {
                              @Override
                              public View getInfoWindow(Marker marker) {
                                  View myContentView = getLayoutInflater().inflate(
                                          R.layout.map_info_window, null);
                                  myContentView.setBackgroundColor(Color.TRANSPARENT);
                                  TextView tvTitle = ((TextView) myContentView
                                          .findViewById(R.id.title));
           tvTitle.setText(marker.getTitle());
                                 ......
                                  return myContentView;
                              }
          
                              @Override
                              public View getInfoContents(Marker marker) {
                                  return null;
                              }
                          };
                      }
          

          【讨论】:

          • 你能告诉我如何在我的代码中使用它吗?我不是一个很好的安卓开发者。 :)
          • 你有一个谷歌地图对象。 googleMapObject.setInfoWindowAdapter(setMarkerWindow());并粘贴上面的函数 setMarkerWindow()。它会正常工作
          • 为什么我不能解决 setInfoWindowAdapter(setMarkerWindow()); ?请查看添加的错误图片。 :)
          • 尝试使用 GoogleMapFragment.getMap()
          • 我的回答对你有帮助吗?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-07-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多