我正在使用 google maps api v2 for android 在 infoWindow 中显示业务详细信息来实现这一点,我使用 asynctask 从服务器加载交易详细信息。当用户单击标记时,需要显示信息窗口。
我的问题是我为所有标记获得相同的信息窗口(相同的数据)我应该怎么做才能在信息窗口中显示标记信息?
另一个问题是由于某种原因, onMarkerClick() 回调不起作用。
我很乐意编写示例代码或告诉我如何使用 infoWindowAdapter 以及为什么 onMarkerClick 不起作用?
public class GetDealsNearbyTask extends AsyncTask<String, Integer, List<Deal>>{
Context context;
Editor editor;
SharedPreferences settings;
Editor settingsEditor;
private FragmentActivity activity;
private UserUtil user;
private GoogleMap map;
HashMap<Marker, Deal> dealMarker;
public GetDealsNearbyTask(Context context, FragmentActivity activity) {
this.context = context;
this.activity = activity;
settings = context.getSharedPreferences(Constants.SETTINGS_FILE_NAME,Context.MODE_PRIVATE);
settingsEditor = settings.edit();
user = new UserUtil(activity);
}
@Override
protected void onPreExecute() {
}
@Override
protected List<Deal> doInBackground(String... urls) {
HttpConnection httpConnection = new HttpConnection(urls[0]);
String currentSecurityToken = settings.getString(Constants.SECURITY_TOKEN, null);
JSONObject json = new JSONObject();
double lat = user.getSelfLocation().getLatitude();
double lon = user.getSelfLocation().getLongitude();
// JSON data:
try {
json.put(Constants.SECURITY_TOKEN, currentSecurityToken);
json.put(Constants.Latitude, lat);
json.put(Constants.Longitude, lon);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Set HTTP parameters
StringEntity se = null;
try {
se = new StringEntity(json.toString());
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
httpConnection.AddParam(se);
httpConnection.AddHeader("Content-type", "application/json");
try {
httpConnection.Execute(RequestMethod.POST);
} catch (Exception e) {
e.printStackTrace();
}
String response = httpConnection.getResponse();
String errorMessage = httpConnection.getErrorMessage();
if (errorMessage.equalsIgnoreCase("OK")){
return saveToPreferences(response);
}else{
return null;
}
}
private List<Deal> saveToPreferences(String response) {
List<Deal> deals = null;
try {
JSONObject responseObject = new JSONObject(response);
String securityToken = (String)responseObject.get(Constants.SECURITY_TOKEN);
settingsEditor.putString(Constants.SECURITY_TOKEN, securityToken).commit();
String status = (String)responseObject.get("Status");
JSONArray results = responseObject.getJSONArray("Results");
deals = new ArrayList<Deal>();
for (int i = 0; i < results.length(); i++) {
JSONObject jDeal = results.getJSONObject(i);
String branchAdress = jDeal.getString(Constants.BranchAdress);
String branchName = jDeal.getString(Constants.BranchName);
String currency = jDeal.getString(Constants.Currency);
int actualPrice = jDeal.getInt(Constants.ActualPrice);
int dealCode = jDeal.getInt(Constants.DealCode);
String discount = jDeal.getString(Constants.Discount);
int distance = jDeal.getInt(Constants.Distance);
String endDateTime = jDeal.getJSONObject(Constants.Ending).getString(Constants.ExpirationDateTime);
int interestCode = jDeal.getInt(Constants.InterestCode);
double lat = jDeal.getDouble(Constants.Latitude);
double lon = jDeal.getDouble(Constants.Longitude);
int ourPick = jDeal.getInt(Constants.OurPick);
int rating = jDeal.getInt(Constants.Rating);
String title = jDeal.getString(Constants.Title);
String smallImageUrl = jDeal.getString("SmallPhoto");
deals.add(new Deal(interestCode, dealCode, rating, title, branchName, smallImageUrl, branchAdress, endDateTime, lat, lon));
}
}catch (JSONException e) {
e.printStackTrace();
}
return deals;
}
@Override
protected void onPostExecute(final List<Deal> data) {
// get reference to GoogleMap
this.map = null;
SupportMapFragment mapFragment = (SupportMapFragment) activity.getSupportFragmentManager().findFragmentById(R.id.map);
this.map = mapFragment.getMap();
// move camera to self location
LatLng selfLocation = new LatLng(user.getSelfLocation().getLatitude(), user.getSelfLocation().getLongitude());
map.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(selfLocation, 10, 0, 0)));
// set self location marker configuration
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(selfLocation );
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.logo));
markerOptions.title("this is you!");
map.addMarker(markerOptions);
// add deals markers
for (final Deal deal : data) {
int markerIcon = R.drawable.ic_menu_preferences;
switch (deal.getInterestCode()) {
case 2:
markerIcon = (R.drawable.books_blue);
break;
case 3:
markerIcon = (R.drawable.rest_blue);
break;
case 4:
markerIcon = (R.drawable.bar_blue);
break;
case 5:
markerIcon = ( R.drawable.electronic_blue);
break;
case 6:
markerIcon = (R.drawable.spa_blue);
break;
case 7:
markerIcon = (R.drawable.sports_blue);
break;
case 8:
markerIcon = (R.drawable.cloth_blue);
break;
case 9:
markerIcon = (R.drawable.coffee_blue);
break;
default:
break;
}
try {
Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(deal.getLat(), deal.getLon())).title("Marker").icon(BitmapDescriptorFactory.fromResource(markerIcon)));
dealMarker.put(marker, deal);
} catch (Exception e) {
Log.e("MAP", "faild adding marker in " + deal.getLat() + " , " + deal.getLon() + " interestCode: " + deal.getInterestCode() + "message: " + e.getMessage());
}
}
// handle click event for the markers
map.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Deal deal = dealMarker.get(marker);
Toast.makeText(context, "the title is: " + deal.getTitle(), Toast.LENGTH_SHORT).show();
GoozInfoWindowAdapter adapter = new GoozInfoWindowAdapter(activity.getLayoutInflater(), deal);
map.setInfoWindowAdapter(adapter);
marker.showInfoWindow();
return false;
}
});
}
private Marker placeMarker(Deal deal) {
Marker m = map.addMarker(new MarkerOptions()
.position(new LatLng(deal.getLat(),deal.getLon()))
.title(deal.getTitle()));
return m;
}
@Override
protected void onProgressUpdate(Integer... progress) {
}