如何在地图上显示所有位置

时间:2014-06-24 09:15:06

标签: android google-maps google-maps-android-api-2 android-maps-v2

如何在地图上显示所有位置,只要用户点击任何列表项,我就 成功显示单个位置 ,但现在我必须使用点击按钮

在地图上显示所有位置

这就是我的JSON的样子:

{
    "search": [
        {
            "country": "India",
            "location": [
                {
                    "title": "New Delhi",
                    "hotel": [
                        {
                "name": "Hotel 5 Star",
                "lat": 28.6413852,
                "lon": 77.1211905,
                "thumb": "UN_HQ.jpg"
                        },
                        {
                "name": "Hotel Premium",
                "lat": 28.6206446,
                "lon": 77.0885432,
                "thumb": "Avery_Fisher_Hall.jpg"
                        }
                    ]
                },
                {
                    "title": "Bangalore",
                    "hotel": [
                        {
                "name": "Hotel Comfort",
                "lat": 12.97159870,
                "lon": 77.59456269,
                "thumb": "Carnegie_Hall.jpg"
                        }
                    ]
                }
            ]
        },
        {
            "country": "USA",
            "location": [
                {
                    "title": "California",
                    "hotel": [
                        {
                "name": "Hotel Zone",
                "lat": 36.7782610,
                "lon": -119.4179323,
                "thumb": "UN_HQ.jpg"
                        }
                    ]
                }
            ]
        }
    ]
}

HotelActivity.java: -

public class HotelActivity extends Activity implements OnItemClickListener {

    ListView listview = null;
    private ArrayList<Location> arrayListLocation;
    private int index;
    private Location location;
    private ArrayList<Hotel> arrayListHotel;

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.listview);

        Button btn = (Button) findViewById(R.id.button1);

        Intent in = getIntent();
        Bundle bundle = in.getBundleExtra("bundle");
        arrayListLocation = (ArrayList<Location>) bundle.getSerializable("data");
        index = bundle.getInt("index");
        location = arrayListLocation.get(index);
        arrayListHotel = location.getHotels();

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(HotelActivity.this, AllMapActivity.class);
                startActivity(intent);
            }
        });

        listview.setAdapter(new ThirdListViewAdapter(HotelActivity.this, arrayListHotel));
        listview.setOnItemClickListener(this);
    }

AllMapActivity.java: -

public class AllMapActivity extends Activity implements
    OnInfoWindowClickListener {
  private static final String STATE_NAV="nav";

  private GoogleMap map=null;
  private HashMap<String, Uri> images=new HashMap<String, Uri>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_map);

      MapFragment mapFrag=
              (MapFragment)getFragmentManager().findFragmentById(R.id.map);

          map=mapFrag.getMap();

          if (savedInstanceState == null) {
            CameraUpdate center=
                CameraUpdateFactory.newLatLng(new LatLng(Double.parseDouble(""),
                                                          Double.parseDouble("")));
            CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

            map.moveCamera(center);
            map.animateCamera(zoom);
      }

      addMarker(map, Double.parseDouble(""), Double.parseDouble(""),
                "", "snippet", "");

      map.setInfoWindowAdapter(new PopupAdapter(this,
                                                getLayoutInflater(),
                                                images));
      map.setOnInfoWindowClickListener(this);
  }

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(STATE_NAV,
                              getActionBar().getSelectedNavigationIndex());
  }

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
  }

  @Override
  public void onInfoWindowClick(Marker marker) {
    Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
  }

  private void addMarker(GoogleMap map, double lat, double lon,
                         String title, String snippet, String image) {
    Marker marker = map.addMarker(new MarkerOptions()
    .position(new LatLng(lat, lon))
    .title(title)
    .snippet(snippet));

    }
  }
}

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

private ArrayList<Hotel> arrayListHotelData = new ArrayList<Hotel>();
private Hotel ItemBean ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_map);

arrayListHotelData=ConstantData.arrayListHotelDataTemp;

MapFragment mapFrag=
(MapFragment)getFragmentManager().findFragmentById(R.id.map);

map=mapFrag.getMap();

if (savedInstanceState == null) {

for (int i = 0; i < arrayListHotelData.size(); i++) {
ItemBean = arrayListHotelData.get(i);
if (ItemBean.getLat().trim().length() > 0 && ItemBean.getLon().trim().length() > 0) {
addMarker(map, Double.parseDouble(ItemBean.getLat()), Double.parseDouble(ItemBean.getLon()),
ItemBean.getName(), "snippet", ItemBean.getThumb());
}
}
} 

答案 1 :(得分:1)

您可以检查它以获得更好的解决方案和处理代码的更好方法..

Intent in = getIntent();
        Bundle bundle = in.getBundleExtra("bundle");
        arraylist = (ArrayList<Hotel>) bundle.getSerializable("viewall");

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        if (status != ConnectionResult.SUCCESS) {
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, AllHotelsMapActivity.this,
                    requestCode);
            dialog.show();

        } else {
            if (googleMap == null) {
                googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                googleMap.setMyLocationEnabled(false);
            }

            for (int i = 0; i < arraylist.size(); i++) {
                Hotel hotel=arraylist.get(i);
                Double lat=hotel.getLat();
                Double lng=hotel.getLon();
                String name=hotel.getName();
                drawMarker(new LatLng(lat, lng),name);
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
            }
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat("2")));
        }
         RadioGroup rgViews = (RadioGroup) findViewById(R.id.radioGroupView);

            rgViews.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == R.id.radio_maps){
                        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    }else if(checkedId == R.id.radio_sat){
                        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                    }else if(checkedId == R.id.radio_terrain){
                        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                    }
                }
            });
    }

    private void drawMarker(LatLng point,String locName) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(point);
        markerOptions.title(locName);
        googleMap.addMarker(markerOptions);
    }