Add the wrong place to mapview?

I am working with Google Map. My problem is that when I touch the screen, the card does not add the pin exactly to the right place. Adding almost Ycoordinate-50.

I added CustomitemzedOverlay too.

What is wrong with the code?

Can someone help me?

code:

public void AddPoint(Drawable drawable, MapView mapView, MotionEvent motionEvent) { p = mapView.getProjection().fromPixels((int) (motionEvent.getX()),(int) (motionEvent.getY())); final MapController mc = mapView.getController(); mc.setZoom(16); CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView); itemizedOverlay.addOverlay(new CustomOverlayItem(p,"","","")); mapView.getOverlays().add(itemizedOverlay); mc.animateTo(p); mapView.invalidate(); } package com.example; import android.content.Context; import android.graphics.drawable.Drawable; import android.widget.Toast; import com.google.android.maps.MapView; import com.google.android.maps.OverlayItem; import java.util.ArrayList; public class CustomItemizedOverlay<Item extends OverlayItem> extends BalloonItemizedOverlay<CustomOverlayItem> { private ArrayList<CustomOverlayItem> m_overlays = new ArrayList<CustomOverlayItem>(); private Context c; public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) { super(boundCenter(defaultMarker), mapView); c = mapView.getContext(); } public void addOverlay(CustomOverlayItem overlay) { m_overlays.add(overlay); populate(); } public void removeOverlay(CustomOverlayItem overlay) { m_overlays.remove(overlay); populate(); } @Override protected CustomOverlayItem createItem(int i) { return m_overlays.get(i); } @Override public int size() { return m_overlays.size(); } @Override protected boolean onBalloonTap(int index, CustomOverlayItem item) { Toast.makeText(c, "onBalloonTap for overlay index " + index, Toast.LENGTH_LONG).show(); return true; } @Override protected BalloonOverlayView<CustomOverlayItem> createBalloonOverlayView() { // use our custom balloon view with our custom overlay item type: return new CustomBalloonOverlayView<CustomOverlayItem>(getMapView().getContext(), getBalloonBottomOffset()); } } 
+4
source share
3 answers

Here's a complete mapview tutorial: comprehensive google map tutorials

u can create an overlay class; and in onDraw() , u should use getIntrinsicHeight() getIntrinsicWidth()/2

There's also a tutorial on overlay detailing where you can use boundCenterBottom() to position your marker correctly.

0
source

I have the same problem as when using android-mapviewballoons . I need half a day to find a solution. Here is my fix:

On your CustomItemizedOverlay let me make an overlay in boundCenterBottom

 public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) { super(boundCenterBottom(defaultMarker), mapView); c = mapView.getContext(); } 

And in your Map Activity, when you create a marker, you can do a trick like this (thanks to Cyril Mottier for his Polaris mapview library ):

 drawable = MapViewUtils.boundMarkerCenterBottom(getResources().getDrawable(R.drawable.map_pin_holed_violet)); 

MapViewUtils obtained from the Polaris library. Here's the boundMarkerCenterBottom function:

 public static Drawable boundMarkerCenterBottom(Drawable marker) { return boundMarker(marker, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); } public static Drawable boundMarker(Drawable marker, int gravity) { if (marker == null) { return null; } final int width = marker.getIntrinsicWidth(); final int height = marker.getIntrinsicHeight(); if (width < 0 || height < 0) { throw new IllegalStateException("The given Drawable has no intrinsic width or height"); } int left, top; switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { case Gravity.LEFT: left = 0; break; case Gravity.RIGHT: left = -width; break; case Gravity.CENTER_HORIZONTAL: default: left = -width / 2; break; } switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { case Gravity.TOP: top = 0; break; case Gravity.CENTER_VERTICAL: top = -height / 2; break; case Gravity.BOTTOM: default: top = -height; break; } marker.setBounds(left, top, left + width, top + height); return marker; } 
0
source

I'm not sure, but I think the problem may be setting the scaling after receiving the projection. I actually do. There is a tutorial on what you want on the blog that I am contributing to AndroidDev101 , which should give you what you need to complete the task by adding also an item.

The blog

0
source

Source: https://habr.com/ru/post/1391269/


All Articles