我知道我站在正确的位置向我的案例研究寻求帮助,然后在几天内我想到是否可以使用Volley Library将数据存储到MapFragment但不幸的是直到现在我找不到解决方案。一般MapFragment使用AsycnTask(如果我错了请教我)。为什么我使用Volley Library因为获得/发布数据客户端服务器的全部功能。你能不能给我一些线索或实施如何解决我的问题。提前致谢
public class MapTestFragment extends Fragment {
private static final String TAG = MapTestFragment.class.getSimpleName();
private static final String url = "myurl";
private List<MapEntity> mbList = new ArrayList<MapEntity>();
private GoogleMap googleMap;
static final LatLng Location = new LatLng(-1.782877, 90.381806);
Marker mark;
private MapTestAdapter adapter;
private HashMap<Marker, MapEntity> mMarkersHashMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
getLatLang();
mMarkersHashMap = new HashMap<Marker, MapEntity>();
try {
// Loading map
initilizeMap();
plotMarkers(mbList);
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
private void getLatLang() {
JsonArrayRequest mapReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// TODO Auto-generated method stub
Log.d(TAG, response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
MapEntity mb = new MapEntity();
mb.setTitle(obj.getString("title"));
mb.setLat(((Number) obj.get("lat"))
.doubleValue());
mb.setLang(((Number) obj.get("lang"))
.doubleValue());
mb.setThumbnail(obj.getString("thumbnail"));
mbList.add(mb);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(mapReq);
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// TODO Auto-generated method stub
marker.showInfoWindow();
return true;
}
});
}
else {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
private void plotMarkers(List<MapEntity> mbList)
{
if(mbList.size() > 0)
{
for (MapEntity myMarker : mbList2)
{
// Create user marker with custom icon and other options
MarkerOptions markerOption = new MarkerOptions().
position(new LatLng(myMarker.getLat(),
myMarker.getLang()));
markerOption.icon(BitmapDescriptorFactory.
fromResource(R.drawable.ic_launcher));
Marker currentMarker = googleMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
//googleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
public class MapTestAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<MapEntity> mapItems;
private TextView tvTitle, tvLat, tvLang;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public MapTestAdapter(Activity activity,
List<MapEntity> mapItems ) {
super();
this.activity = activity;
this.mapItems = mapItems;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mapItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mapItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (inflater == null)
inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.fragment_map_custom_marker, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
tvTitle = (TextView) convertView.findViewById(R.id.txtTitleMarker);
tvLat = (TextView) convertView.findViewById(R.id.txtLatMarker);
tvLang = (TextView) convertView.findViewById(R.id.txtLangMarker);
NetworkImageView imageshow = (NetworkImageView)
convertView.findViewById(R.id.markerthumnail);
final MapBerau mb = mapItems.get(position);
tvTitle.setText(mb.getTitle());
tvLat.setText(String.valueOf(mb.getLat()));
tvLang.setText(String.valueOf(mb.getLang()));
imageshow.setImageUrl(mb.getThumbnail(), imageLoader);
return convertView;
}
}
答案 0 :(得分:3)
要使用以下代码放置多个标记:
private HashMap<Marker, HashMap<String, String>> markerHashMap;
markerHashMap = new HashMap<Marker, HashMap<String, String>>();
//Here, my data format is ArrayList<HashMap<String, String>>;
for (int i = 0; i < YOUR DATA.size(); i++) {
final HashMap<String, String> mapData = YOURDATA.get(i);
if (mapData.get("latitude").trim().length() > 0 && mapData.get("longitude").trim().length() > 0) {
placeMarker(mapData);
}
}
//placeMarker() method:
private void placeMarker(final HashMap<String, String> mapData) {
private Marker marker;
try {
marker=googleMap.addMarker(new MarkerOptions().icon(
BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.YOUR MARKER IMAGE))).position(
new LatLng(Double.parseDouble(mapData.get("latitude")), Double.parseDouble(mapData.get("longitude")))));
markerHashMap.put(marker, mapData);
} catch (Exception e) {
e.printStackTrace();
}
}