【问题标题】:Wrong value for marker text passed to custom view传递给自定义视图的标记文本值错误
【发布时间】:2015-04-01 06:32:24
【问题描述】:

我正在开发一个显示地图的 android 应用程序。当它加载时,它会显示一些地址并为它们设置标记。

当我点击任何标记时,它应该在自定义视图中显示一个值。但是从 json 解析器接收到的自定义文本会得到一个空值。当我再次单击标记时,它会设置正确的值。

当我点击第二个标记时,它会显示第一个标记值。当我再次单击第二个标记时,它显示正确的值。这个过程还在继续

这是我的代码:

    private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
        private Context mainContxt;
        Geocoder geocoder;
        public GeocoderTask(Context con){
        mainContxt=con;

        } 

        @Override
        protected List<Address> doInBackground(String... locationName) {
             Geocoder geocoder = new Geocoder(mainContxt);
                List<Address> addresses = null;
                try {
                    addresses = geocoder.getFromLocationName(locationName[0],1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return addresses;
        }


        @Override
        protected void onPostExecute(List<Address> addresses) { 

       for(int i=0;i<addresses.size();i++){             

                Address address = (Address) addresses.get(i);
                latLng = new LatLng(address.getLatitude(), address.getLongitude());

                String addressText = String.format("%s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                        address.getCountryName());

                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title(addressText);
                if(i==0)    {
                    googleMap.animateCamera(CameraUpdateFactory.zoomBy(14),2000,null);
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
                }
                googleMap.addMarker(markerOptions);

            }
       googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker_address) {
                    location=marker_address.getTitle();
                    Toast.makeText(getApplicationContext(),location, Toast.LENGTH_LONG).show();
                new LoadSingleProperty().execute();
                //new LoadImage().execute();
                return false;

            }
        });

        googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }
            @Override
            public View getInfoContents(Marker marker) {

                View myContentView = getLayoutInflater().inflate(
                        R.layout.custom_marker, null);
                tempnew_price=getPrice(temptotal_price+"" +email);
                TextView tvTitle = ((TextView) myContentView
                        .findViewById(R.id.title));
               // tvTitle.setText(location);
                tvSnippet = ((TextView) myContentView
                        .findViewById(R.id.snippet));

               ivProperty = ((ImageView) myContentView
                        .findViewById(R.id.image_property));

               tvTitle.setText(tempcovered_area+ " "+tempnew_price+System.getProperty("line.separator")+templocation);

               tvSnippet.setText("A "+ tempbedroom + " "+tempproperty_type);


              // new LoadImage().execute();
            ivProperty.setImageBitmap(bmp); 
                    return myContentView;


            }
        });
        googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker arg0) {
                Intent intent = new Intent(getBaseContext(),
                        search_property_activity.class);
                intent.putExtra("Email", email);
                startActivity(intent);
            }
        });
    }
    }

这是我的加载单类编码.....

    class LoadSingleProperty extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivityMap.this);
            pDialog.setMessage("Loading Location. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String doInBackground(String... args) {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
            if(location!=null && !location.equals("")){
                params.add(new BasicNameValuePair("Location", location));
                json= jsonParser.makeHttpRequest(url_loc_address, "GET", params);
            }
            Log.d("MyLocation: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    address = json.getJSONArray(TAG_ALL_ADDRESS);
                    //for (int i = 0; i < address.length(); i++) {
                        JSONObject c = address.getJSONObject(0);
                        templocation = c.getString(TAG_LOCATION);
                        tempcovered_area=c.getString(TAG_COVERED_AREA);
                        temptotal_price=c.getString(TAG_Total_Price);

                        tempbedroom=c.getString(TAG_BEDROOM);
                        tempproperty_type=c.getString(TAG_PROPERTY_TYPE);
                        tempemail=c.getString(TAG_EMAIL);
                        //} 
                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }



    protected void onPostExecute(String file_url) {
        pDialog.dismiss();  
        new GeocoderTask(MainActivityMap.this).execute(location);
        }


    }

帮助我的朋友...提前谢谢

【问题讨论】:

  • 如果有人有答案然后帮助我????
  • 如果我调用 new GeocoderTask(MainActivityMap.this).execute(location);从背景加载单个属性...没有任何改变
  • 嗨.....我的建议是......看看你在'位置'中得到了什么价值......?我觉得这里出了点问题?
  • @gvsharma 这是我第一次点击时的 json 响应 04-01 12:39:10.469: D/MyLocation:(2534): {"address_all":[{"email_id":"asddd @a.bn","location":"印度贾坎德邦博卡罗钢铁城","covered_area":"100sq-m","sell_type":"SELL","property_type":"House","total_price": "789","bedroom":"3BHK"}],"success":1} 那个时候自定义视图的所有字段都为空
  • 在你的 json 解析完成之前检查标记的信息窗口是否正在显示

标签: android json google-maps android-asynctask android-location


【解决方案1】:

为 LoadSingleProperty 创建一个构造函数

class LoadSingleProperty extends AsyncTask<String, String, String> {
    Marker mMarker;

    public LoadSingleProperty(Marker marker){
      mMarker = marker;
    }

    .
    .
    .
}

并将您的标记对象传递给它。

new LoadSingleProperty(marker_address).execute();

解析完成后,使用标记的setTitle() 方法设置标记的标题

mMarker.setTitle(c.getString(TAG_PROPERTY_TYPE));

您当前执行此操作的位置

tempproperty_type=c.getString(TAG_PROPERTY_TYPE);

标题重置后不要忘记刷新信息窗口

How to force refresh contents of the markerInfoWindow

您可能还想在此时显示某种加载图标,因为您正在执行网络请求。

编辑:

如果您使用自定义标题视图,请在设置为 googleMap 的适配器之前获取对 InfoWindowAdapter 对象的引用

InfoWindowAdapter infoWindowAdapter = new InfoWindowAdapter() {...

解析完成后,通过调用获取 mMarker 对象的信息窗口视图

infoWindowAdapter.getInfoWindow(mMarker);

从上面获得的视图中找到你的 textView 并设置它的文本。然后通过调用 showInfoWindow() 来刷新您的信息窗口以更新信息窗口。

也请参考这个link

绘制的信息窗口不是实时视图。视图被渲染 在返回时作为图像(使用 View.draw(Canvas))。这 意味着对视图的任何后续更改都不会反映在 地图上的信息窗口。稍后更新信息窗口(对于 例如,加载图像后),调用 showInfoWindow()

【讨论】:

  • 嘿 @random 但我必须使用 tem string bcz 我想在其他文本视图上设置它
【解决方案2】:
private class GeocoderTask extends AsyncTask<String, Void, List<Address>> {
    private Context mainContxt;
    Geocoder geocoder;

    public GeocoderTask(Context con) {
        mainContxt = con;

    }

    @Override
    protected List<Address> doInBackground(String... locationName) {
        Geocoder geocoder = new Geocoder(mainContxt);
        List<Address> addresses = null;
        try {
            addresses = geocoder.getFromLocationName(locationName[0], 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {

        for (int i = 0; i < addresses.size(); i++) {

            Address address = (Address) addresses.get(i);
            latLng = new LatLng(address.getLatitude(),
                    address.getLongitude());

            String addressText = String.format(
                    "%s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "", address
                            .getCountryName());

            markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(addressText);
            if (i == 0) {
                googleMap.animateCamera(CameraUpdateFactory.zoomBy(14),
                        2000, null);
                googleMap.animateCamera(CameraUpdateFactory
                        .newLatLng(latLng));
            }
            googleMap.addMarker(markerOptions);
        }
    }
}

然后你写下面的代码

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker marker_address) {
        location = marker_address.getTitle();
        new LoadSingleProperty().execute();

        return false;

    }
});

获取另一个 customeWindowAdapter 或者仅使用当前的一个,并将其设置在 'LoadSingleProperty' AsyncTask 的 onPostExecute() 方法中。 这将解决这个问题。 ping 这里你有任何疑问

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多