2016-07-25 72 views
2

我在製作Android應用程序。在這個應用程序,有一些酒店的標記,當用戶點擊標記信息窗口出現,它應該像下面的圖像。 (這是一個Web應用程序)爲Android標記信息窗口添加圖像

Marker info window

我有一個列表視圖中我的導航抽屜片段和映射片段具有標記。

這是我的代碼,然後我嘗試在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,其中的任何一個都沒有給我一個解決方案。

有人可以幫助我嗎?

在此先感謝。 :)

  • 錯誤 -

error

回答

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; 
    } 
} 

現在按預期工作。 :)

1

試試這個代碼:

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)); 

        } 
       } 
      } 
     }); 
1

我曾嘗試爲我的應用程序做同樣的。我無法將圖像加載到標記中,因爲我想要。

所以我做的是。

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

對我的要求是在中心標記的信息窗口。你可以將它設置在你想要的地方。

  • 爲了得到標記的假象,我總是設置經緯度,我想是地圖的中心(CameraPosition目標被設定爲經緯度我想)
  • 1

    嘗試使用信息窗口適配器。這可能會幫助你,我想。

    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; 
            } 
           }; 
          } 
    
    +0

    你能告訴我如何使用它與我的代碼?我不是一個很好的android開發人員。 :) –

    +0

    你有一個谷歌地圖對象的權利。 googleMapObject.setInfoWindowAdapter(setMarkerWindow()); 並粘貼上述函數setMarkerWindow()。它會正常工作 – Nivedh

    +0

    爲什麼我無法解決setInfoWindowAdapter(setMarkerWindow()); ?請參閱添加的錯誤圖像。 :) –

    相關問題