作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 map 上显示所有位置,每当用户点击任何列表项时,我都成功显示单个位置,但是现在我必须使用点击按钮显示 map 上的所有位置
这是我的 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));
}
}
}
最佳答案
试试下面的代码:
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());
}
}
}
关于android - 如何在 map 上显示所有位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382748/
我是一名优秀的程序员,十分优秀!