gpt4 book ai didi

android - 在 Android 上显示标记弹出窗口, native 方式

转载 作者:行者123 更新时间:2023-11-30 04:28:07 25 4
gpt4 key购买 nike

我想在 Android 上的 MapView 中的某些标记顶部放置一个弹出窗口,类似于 iPhone 的标注。我到处都看到每个人都推荐第 3 方 API 来做到这一点?

没有本地方法可以做到这一点吗?我需要放置一些基本信息,例如文本和按钮,我的首要任务是在每部 Android 手机上尽可能做到原生,因为它们或多或少都有所不同。

这是本地 map 应用程序的示例:

enter image description here

最佳答案

是的,它在 Android 中要容易得多。这是代码;

在您的 MapActivity 类中声明此变量(或者最好在互联网上阅读有关此变量的信息)

    private List<Overlay> mapOverlays;
private CustomItemizedOverlay customItemizedOverlay;

在你的 onCreate() 中

    mapOverlays = mapView.getOverlays();
customItemizedOverlay = new CustomItemizedOverlay(
GoogleMapsActivity.this);
if (arrayList != null) {
//here Marker is my custom class which contains GeoPoints
for (Marker marker : arrayList) {
GeoPoint point = new GeoPoint(
(int) (marker.getLat() * 1E6),
(int) (marker.getLng() * 1E6));

OverlayItem overlayitem = new OverlayItem(point,
marker.getTitle(), marker.getDescription());
customItemizedOverlay.addOverlay(overlayitem);
}
//finally add all GeoPoints to the mapOverlays
mapOverlays.add(customItemizedOverlay);
if (arrayList.size() > 0) {
GeoPoint geoPoint = new GeoPoint((int) (arrayList.get(1)
.getLat() * 1E6),
(int) (arrayList.get(1).getLng() * 1E6));
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO) {
mapController.animateTo(geoPoint, new Message());
}
}

public MapView getMapView() {
return this.mapView;
}

这是我的 CustomItemizedOverlay 类的代码(它可能会吓到你)

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>
implements OnKeyListener {
private final Bitmap bitMap;
private ViewFlipper viewFlipper;
private final ArrayList<OverlayItem> overLayItems = new ArrayList<OverlayItem>();
private final GoogleMapsActivity googleMapsActivity;
public int selectedIndex = -1;
private View view = null;
private RelativeLayout relativeLayout = null;
private static WebView webView;

public CustomItemizedOverlay(GoogleMapsActivity googleMapsActivity) {
super(boundCenterBottom(googleMapsActivity.getResources().getDrawable(
R.drawable.pingreen)));
this.googleMapsActivity = googleMapsActivity;
bitMap = BitmapFactory.decodeResource(
googleMapsActivity.getResources(), R.drawable.pingreen);
populate();
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
if (overLayItems == null || overLayItems.isEmpty()) {
return false;
}
return super.draw(canvas, mapView, false, when);
}

public void addOverlay(OverlayItem overlay) {
overLayItems.add(overlay);
populate();
}

@Override
protected OverlayItem createItem(int index) {
return overLayItems.get(index);
}

@Override
public int size() {
return overLayItems.size();
}

public void refresh() {
populate();
}

public void clear() {
overLayItems.clear();
resetLastFocuesIndex();
}

@Override
protected boolean onTap(final int index) {
googleMapsActivity.getMapView().setOnTouchListener(
new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (!overLayItems.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
}
}
googleMapsActivity.getMapView().invalidate();
return false;
}
});
if (view != null) {
view.setVisibility(View.GONE);
googleMapsActivity.getMapView().removeView(view);
googleMapsActivity.getMapView().invalidate();
view = null;
}
view = googleMapsActivity.getLayoutInflater().inflate(
R.layout.popupwindow, null);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.popupwindow);
ImageView image = (ImageView) view.findViewById(R.id.imageview);
TextView text = (TextView) view.findViewById(R.id.text);
text.setText(overLayItems.get(index).getTitle());

if (overLayItems.get(index).getTitle() != null
&& overLayItems.get(index).getTitle().equals("Me") == false) {
image.setImageResource(R.drawable.wwwpd);
}

Projection projection = googleMapsActivity.getMapView().getProjection();
Point point = new Point();
projection.toPixels(overLayItems.get(index).getPoint(), point);

int x = (int) (view.getWidth() / 2f);
int y = -bitMap.getHeight() - 3;

MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, overLayItems.get(index)
.getPoint(), x, y, MapView.LayoutParams.BOTTOM_CENTER);
googleMapsActivity.getMapView().removeView(view);
googleMapsActivity.getMapView().invalidate();
googleMapsActivity.getMapView().addView(view, lp);
googleMapsActivity.getMapView().invalidate();

view.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if (Common.haveNetworkConnection(googleMapsActivity)) {
googleMapsActivity.getMapView().removeView(view);
googleMapsActivity.getMapView().invalidate();
view = null;
String desription = googleMapsActivity.getMarkersList()
.get(index).getDescription();
String str1 = desription.substring(
desription.indexOf("\"") + 1, desription.length());
String str2 = str1.substring(0, str1.indexOf("\""));

viewFlipper = (ViewFlipper) googleMapsActivity
.findViewById(R.id.flipper);
viewFlipper.setDisplayedChild(2);

relativeLayout = (RelativeLayout) googleMapsActivity
.findViewById(R.id.relativeLayout);
Button button = new Button(googleMapsActivity);
button.setText("Back");
button.setId(1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params1);
button.setVisibility(View.GONE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewFlipper.setDisplayedChild(0);
}
});
relativeLayout.addView(button);

webView = new WebView((Context) (googleMapsActivity
.getParent() == null ? this : googleMapsActivity
.getParent()));
webView.setId(2);
webView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
// webView.getSettings()
// .setUserAgentString(
// "Mozilla/5.0 (Linux; U; Android 2.3.3; en-au; GT-I9100 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.12011-10-16 20:22:55");

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
webView.setLayoutParams(params2);
params2.addRule(RelativeLayout.BELOW, 1);
relativeLayout.addView(webView);

webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);

}

@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
viewFlipper.setDisplayedChild(2);
}

@Override
public void onPageFinished(WebView view, String url) {
viewFlipper.setDisplayedChild(1);
webView.requestFocus(View.FOCUS_DOWN);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}

@Override
public void onReceivedError(WebView view,
int errorCode, String description,
String failingUrl) {
super.onReceivedError(view, errorCode, description,
failingUrl);
viewFlipper.setDisplayedChild(0);
}
});

webView.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View view, int keyCode,
KeyEvent keyEvent) {
if ((keyCode == KeyEvent.KEYCODE_BACK)
&& webView.canGoBack()) {
webView.goBack();
return true;
}
googleMapsActivity.finish();
return true;
}
});
webView.loadUrl(str2);
}
}
});
selectedIndex = index;
return true;
}

public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
selectedIndex = -1;
}

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
viewFlipper.showPrevious();
return true;
}
return true;
}
}

关于android - 在 Android 上显示标记弹出窗口, native 方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8151905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com