Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:44:55+00:00 2026-05-24T03:44:55+00:00

I have a map activity which displays the map, I want to add a

  • 0

I have a map activity which displays the map, I want to add a marker when I touch on the screen ..

This is my Activity …

package com.adhamenaya.android;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapApp extends MapActivity {

    private MapView mapView;
    private MapController mapController;
    private LocationManager locationManager;
    private Context context;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context=this;
        initLayout();
        initMap();
    }

    private void initLayout(){
         mapView = (MapView) findViewById(R.id.mapview);
         //blueIcon = (Drawable)this.getResources().getDrawable(R.drawable.blueicon);

    }
    private void initMap(){
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        mapController=mapView.getController();
        mapController.setZoom(10);// 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());

    }


    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            int lat=(int)(location.getLatitude()*1E6);
            int lng=(int)(location.getLongitude()*1E6);

            GeoPoint point=new GeoPoint(lat,lng);
            //GeoPoint point = new GeoPoint(19240000,-99120000);

            mapController.animateTo(point);
            mapController.setCenter(point);
            Drawable redIcon = context.getResources().getDrawable(R.drawable.redicon);
            List<Overlay> mapOverlays = mapView.getOverlays();
            MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
            OverlayItem overlayitem = new OverlayItem(point, "Hello !", "I'm here");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
        public boolean onTouchEvent(MotionEvent event){
            int x=(int)event.getX();
            int y=(int)event.getY();
            Toast.makeText(context, x, Toast.LENGTH_LONG).show();
            GeoPoint point = mapView.getProjection().fromPixels(x, y);

            Drawable redIcon = context.getResources().getDrawable(R.drawable.blueicon);
            List<Overlay> mapOverlays = mapView.getOverlays();
            MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
            OverlayItem overlayitem = new OverlayItem(point, "New Place", "I clicked here !");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);
            return false;
        }

}
}

Edit : This is the class for ItemizedOverlay, where I have implemented onTap() method , but nothing happens …

package com.adhamenaya.android;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay{

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    public MyItemizedOverlay(Drawable defaultMarker) {
          super(boundCenterBottom(defaultMarker));
    }

    public MyItemizedOverlay(Drawable defaultMarker, Context context) {
          super(defaultMarker);
          mContext = context;
    }
    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }
    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }
    @Override
    public int size() {
        return mOverlays.size();
    }
    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }
     @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {
         int x=(int)event.getX();
            int y=(int)event.getY();
            Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
            return super.onTouchEvent(event, mapView);
        }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:44:57+00:00Added an answer on May 24, 2026 at 3:44 am

    Hello, MapView! tutorial covers showing markers on MapView. In the tutorial markers are added as soon as the activity starts, but you can also add markers at later time, for example, when user touches screen. If you try this approach, you’ll likely want to override onTap in your subclass of ItemizedOverlay.

    Update:

    If you follow the tutorial, you’ll have your own subclass of ItemizedOverlay. onTap is one of its methods. Now that I look at the documentation, unfortunately onTap(GeoPoint p, MapView mapView) is not public or protected so you cannot override it.

    What you can do, is have another overlay, solely for detecting taps. Instead of subclassing ItemizedOverlay, subclass Overlay.

    private class TapOverlay extends Overlay {
        public boolean onTap(GeoPoint p, MapView mapView) {
            // code that adds item in your ItemizedOverlay
            // goes here
    
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.