我使用google maps api v2 for android在infoWindow中显示商家详情 为了实现这一点,我使用asynctask从服务器加载交易细节。 当用户点击标记时,infowindow需要显示。
我的问题是我获得了所有标记的相同信息(相同的数据) 我该怎么做才能在信息窗口中显示标记信息?
其他问题是,由于某些原因,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) {
}
答案 0 :(得分:0)
看一下InfoWindowAdapter
的代码,有评论说明几乎每行都做了什么。
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker args) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
clickMarkerLatLng = args.getPosition();
TextView title = (TextView) v.findViewById(R.id.tvTitle);
title.setText(args.getTitle());
//Setting the click listener
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker)
{
//Check if the clicked marker is my location
if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
{
if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.", Toast.LENGTH_SHORT).show();
}
else
{
FlurryAgent.onEvent("Start navigation window was clicked from daily map");
tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
for (Task tmptask : tasksRepository)
{
String tempTaskLat = String.valueOf(tmptask.getLatitude());
String tempTaskLng = String.valueOf(tmptask.getLongtitude());
Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));
if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
task = tmptask;
break;
}
}
Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
intent.putExtra(TasksListActivity.KEY_ID, task.getId());
startActivity(intent);
}
}
else
{
Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.", Toast.LENGTH_SHORT).show();
}
}
});
// Returning the view containing InfoWindow contents
return v;
}
});
答案 1 :(得分:0)
我在使用map.setInfoWindowAdapter时遇到了同样的行为(同一个infowindow对所有标记使用相同的数据)这就是为我解决的问题:经过一些调试后,我发现当用户完成对setInfoWindowAdapter()的调用时点击标记。对我来说,我用于在视图中设置属性的对象不再在范围内。我更改了代码,以便在运行时获取对象,然后在视图中设置属性。
map.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoContents(Marker marker) {
View view = instance.getLayoutInflater(getArguments()).inflate(R.layout.my_infowindow, null);
TextView row1 = (TextView)view.findViewById(R.id.row1);
TextView row2 = (TextView)view.findViewById(R.id.row2);
// Get the object so you can use it to set properties in the view
// In my case I have an ArrayList of Objects. One of the properties of the object is a String that is the same as marker.getTitle() but whatever you need to do, get your runtime data:
Object myObj = getMyObject(marker.getTitle);
row1.setText(myObj.getPropertyOne());
row2.setText(myObj.getPropertyTwo());
return view;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
});