为Android标记信息窗口添加图片 [英] Add an image for Android marker info window

查看:96
本文介绍了为Android标记信息窗口添加图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Android应用程序。在这个应用程序,有一些酒店的标记,当用户点击标记信息窗口出现,它应该像下面的图像。 (这是一个web应用程序)





我有我的导航抽屉片段和地图片段中的列表视图具有标记。



这是我的代码,然后我尝试为 MainActivity.java 。

  //载入酒店
私人课程GetHotelTask​​延伸AsyncTask<字符串,字符串,字符串> {
@Override
protected String doInBackground(String ... data){
String identifier = data [0];

// Toast.makeText(getContext(),Manuli+标识符,Toast.LENGTH_LONG).show();
//创建一个新的HttpClient和Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(http://api.ayubo.lk/android/retrievehotels);
HttpResponse response = null;
String responseStr = null;

尝试{
//添加数据
列表< 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));
//执行http post
response = httpclient.execute(httppost);
responseStr = EntityUtils.toString(response.getEntity());

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

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

return responseStr;
}

保护无效onPostExecute(字符串结果){

尝试{
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 (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(longitude));

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

GoogleMapsFragment.map.animateCamera(CameraUpdateFactory.zoomTo(15)); (JSON解析器,错误解析数据+ e.toString());

} catch(JSONException e){
Log.e
}

}

}



<这里是 MainActivity.java 中的酒店listview调用。

  //酒店
案例0:
if(A == 0){
Bundle bundle = new Bundle();
bundle.putString(restaurants,Default);
fragmentObj = new GoogleMapsFragment();
fragmentObj.setArguments(bundle);如果(!sclItems.contains(0)){
new GetHotelTask​​()。execute(longitude,latitude);


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

我试着添加 info_window_layout.xml 这里是代码。

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_content
android:layout_height =wrap_content
android:orientation =horizo​​ntal>

< 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$ b $ 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>

但是,我无法像载入图像一样加载我的信息窗口。我已经尝试了几种解决方案,包括

解决方案

终于想出来了。 :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
私有LayoutInflater inflater;}

  
私有上下文上下文;

public MapInfoWindowAdapter(Context context){
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;

$ b @Override
public View getInfoWindow(Marker marker){
//从布局文件获取视图
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;
}
}

现在按预期工作。 :)

I'm making an android app. In this app, there are markers of some hotels and when user click on the marker info window appears and it should be like following image. (This is a web application)

I have a list view in my navigation drawer fragment and map fragment has the markers.

This is my code before I try to load image for the info window in 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());
            }

        }

    }

Here is the listview call of hotels inside MainActivity.java.

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

I've tried adding info_window_layout.xml and here is the code.

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

But, I cannot load my info window like in the image. I've tried several solutions including this and any of that didn't give me a solution.

Can someone please help me with this?

Thanks in advance. :)

  • Error -

解决方案

Figured it out finally. :D

Changed the info_window_layout.xml like this.

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

Then added a new class called, 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;
    }
}

Now working as expected. :)

这篇关于为Android标记信息窗口添加图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆