本文介绍了无法在Google地图上看到多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Google地图上显示多个标记。我的latlng从Parse数据库中提取,但我无法看到标记
,而我的第二个问题是我想显示带有标记的餐馆名称的标题,以及我如何做到这一点。
这是我的代码

 
private class putMarker extends AsyncTask> {
$ b $ @Override
保护ArrayList doInBackground(Void ... params){
// TODO自动生成的方法存根
尝试{

Toast.makeText(getApplicationContext(),
经度++纬度,Toast.LENGTH_SHORT).show();

ParseQuery query = new ParseQuery(
Details);
ParseGeoPoint myGeoPiont =新的ParseGeoPoint(纬度,
经度);
query.whereNear(location,myGeoPiont);
query.setLimit(10);
ob = query.find();
for(ParseObject resObj:ob){
ParseGeoPoint location = resObj
.getParseGeoPoint(location);
restaurantName =(String)resObj.get(RestaurantName);
LatLng resLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
Toast.makeText(getApplicationContext(),
restaurantName,Toast.LENGTH_SHORT)
.show();
PiontList.add(resLatLng);
}

} catch(Exception e){
// TODO:处理异常
}
return PiontList;

protected void onPostExecute(ArrayList latlngList){
for(LatLng res:latlngList)
{
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(res);
markerOptions.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions);
}
}

}

请帮助我。

解决方案

我在这里看到一些问题。

正如@Raghunandan提到的,你不能从doInBackground()更新UI,所以你不能从那里添加标记。然而,您可以在这里制作MarkerOptions对象,然后将它们附加到您的postExecute中的GoogleMap中或承载Google地图的活动中。



在onPostExecute()中,您没有为标记设置任何Title或Snippet。每当你创造你的标记,一定要设置你的标题。然后,当用户点击标记时,默认行为会将您的其余名称显示为标题。
代码将是这样的(也可以通过@Inzimam Tariq IT提到:

  MarkerOptions markerOptions = new MarkerOptions( ); 
markerOptions.position(res)
.setTitle(restaurantName)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions);


I want to show multiple marker on google map. My latlng are fetched from Parse database but i am not able see markerand my second problem is i want to show title that is Restaurant Name with marker and how i can do this.This is my code

private class putMarker extends AsyncTask> {

        @Override
        protected ArrayList doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {

                Toast.makeText(getApplicationContext(),
                        longitude + " " + latitude, Toast.LENGTH_SHORT).show();

                ParseQuery query = new ParseQuery(
                        "Details");
                ParseGeoPoint myGeoPiont = new ParseGeoPoint(latitude,
                        longitude);
                query.whereNear("location", myGeoPiont);
                query.setLimit(10);
                ob = query.find();
                for (ParseObject resObj : ob) {
                    ParseGeoPoint location = resObj
                            .getParseGeoPoint("location");
                    restaurantName = (String) resObj.get("RestaurantName");
                    LatLng resLatLng = new LatLng(location.getLatitude(),
                            location.getLongitude());
                    Toast.makeText(getApplicationContext(),
                            restaurantName, Toast.LENGTH_SHORT)
                            .show();
                    PiontList.add(resLatLng);
                }

            } catch (Exception e) {
                // TODO: handle exception
            }
            return PiontList;
        }
        protected void onPostExecute(ArrayList latlngList) {
            for(LatLng res: latlngList)
            {
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(res);
                markerOptions.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                googleMap.addMarker(markerOptions);
            }
        }

    }

Please help me out.

解决方案

I see a few problems here.

As @Raghunandan mentioned, you cannot update the UI from doInBackground() so you cannot add markers from there. You can however, make your MarkerOptions objects here, and then attach them to the GoogleMap in your postExecute/or in the Activity that hosts the Google Maps.

In your onPostExecute(), you have not set any Title, or Snippet to your markers. Whenever you are creating your marker, make sure to set your title. Then when the user clicks on the marker, the default behavior shows your rest name as a title.Code will be something like this(as also mentioned by @Inzimam Tariq IT :

MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(res)
                .setTitle(restaurantName)
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                googleMap.addMarker(markerOptions);

这篇关于无法在Google地图上看到多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:23