As we all know in the great map of the Android SDK, Infowindow is globally unique, and this infowindow related to AMAP and marker, is not very flexible to use, and can not achieve the following similar requirements: Now we have some custom marker, or query the POI data, we want to visually see the point on the map (the effect is shown), rather than need to click on each marker, pop up a infowindow to see its message. We need to consider other ways to achieve this requirement.
In fact, the realization of this function is also very simple, we need to work on, from marker to start rather than from Infowindow to deal with. We know that adding a marker to a map requires Bitmapdescriptor objects, and Bitmapdescriptor objects can be instantiated by bitmapdescriptorfactory, and the instantiation method is as follows:
Fromview This method is the core of our approach to this function, from the name we can infer that this method allows us to initialize a marker with a specific view, so that we can do this function.
Let's first look at the core code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
//位置信息
double latitude =
39.908127
;
double longtitude =
116.375257
;
//显示的内容
String tile =
"一眼就知道我是谁了吧"
;
//初始化marker内容
MarkerOptions markerOptions =
new MarkerOptions();
//这里很简单就加了一个TextView,根据需求可以加载复杂的View
TextView textView =
new TextView(getApplicationContext());
textView.setText(tile);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
15
);
textView.setTextColor(Color.BLACK);
textView.setBackgroundResource(R.drawable.custom_info_bubble);
//通过View获取BitmapDescriptor对象
BitmapDescriptor markerIcon = BitmapDescriptorFactory
.fromView(textView);
markerOptions.position(
new LatLng(latitude, longtitude))
.icon(markerIcon).title(tile).perspective(
true
);
//添加到地图上
aMap.addMarker(markerOptions);
|
Basic code as above, let's look at the effect:
And you can change the style according to your own needs:
It's also possible (you can customize what you want with your own needs)
Finally, an example, the specific application scenario for me to inquire about the surrounding group purchase information, I would like to be able to visually see which stores, OK to start our example:
The basic interface is as follows:
When you click to search around the group, will be the center of Xidan Square, search its surrounding 1000 meters within the range of dining deals, the final effect such as:
Let's take a look at the core code:
Click the Search Peripheral button to query the perimeter:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public void onClick(View v) {
mCurrentPage =
0
;
// 搜索北京市内的餐饮
mQuery =
new Query(
""
,
"餐饮"
,
"010"
);
// 每页显示10条结果
mQuery.setPageSize(
10
);
// 查询的页数
mQuery.setPageNum(mCurrentPage);
// 有团购
mQuery.setLimitGroupbuy(
true
);
mPoiSearch =
new PoiSearch(getApplicationContext(), mQuery);
// 搜索设置的中心周围1000米范围内的团购
mPoiSearch.setBound(
new SearchBound(mSearchCenterPoint,
1000
,
true
));
mPoiSearch.setOnPoiSearchListener(getOnPoiSearchListener());
mPoiSearch.searchPOIAsyn();
}
|
The following processing is done in the query results:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/**
* 处理poi搜索结果
*/
private void processPoiSearchResult(PoiResult poiResult,
int resultCode) {
if (resultCode ==
0
) {
if (poiResult !=
null && poiResult.getQuery() !=
null
) {
// 搜索poi的结果
List<PoiItem> poiItems = poiResult.getPois();
// 取得poiitem数据
if (poiItems !=
null && poiItems.size() >
0
) {
mAMap.clear();
// 清理之前的图标
//继承自PoiOverlay
ViewPoiOverlay poiOverlay =
new ViewPoiOverlay(mAMap,
poiItems, getApplicationContext());
poiOverlay.removeFromMap();
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
mNextPageButton.setClickable(
true
);
// 设置下一页可点
}
}
}
}
|
Where Viewpoioverlay is inherited from the Poioverlay class, the main rewrite is the getbitmapdescriptor (int index) method.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
protected BitmapDescriptor getBitmapDescriptor(
int index) {
TextView textView =
null
;
//缓存View,减少系统消耗
if (views.size() <= index || views.get(index) ==
null
) {
textView =
new TextView(mContext);
String tile = getTitle(index);
textView.setText(tile);
textView.setBackgroundResource(R.drawable.custom_info_bubble);
textView.setTextColor(Color.BLACK);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
15
);
views.add(textView);
}
else {
textView = (TextView) views.get(index);
String tile = getTitle(index);
textView.setText(tile);
}
return BitmapDescriptorFactory.fromView(textView);
}
|
Specific code (don't forget to replace key) and apk see attachment.
PS: In fact, according to this example to divergent ideas, for this effect we can also have different ideas to achieve, but also to achieve more results. For example, we can frombitmap () to instantiate a bitmapdescriptor so that we can draw with canvas and draw the shapes and styles we need to add to the map as marker. So this marker can have countless possibilities, you can give full play to the imagination to achieve the desired effect.
As we all know in the great map of the Android SDK, Infowindow is globally unique, and this infowindow related to AMAP and marker, is not very flexible to use, and can not achieve the following similar requirements: Now we have some custom marker, or query the POI data, we want to visually see the point on the map (the effect is shown), rather than need to click on each marker, pop up a infowindow to see its message. We need to consider other ways to achieve this requirement.