Question

I am trying to draw some icon on the map as long as it is touched. I think I am almost there. Here is the code.

package com.example.googledemo4;

import java.util.List;

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 android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class GoogleDemo4 extends MapActivity{

MapController mc;
MapView mapView;
GeoPoint g;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_demo4);

    mapView=(MapView) findViewById(R.id.MapView);
    mapView.setBuiltInZoomControls(true);


    mc=mapView.getController();

    List<Overlay> overlays = mapView.getOverlays();

    GeoPoint geoPoint = new GeoPoint( 18533000,73850000);


    MapOverlay4 mapOverlay = new MapOverlay4(geoPoint,mapView,this);

    overlays.add(mapOverlay);

    mc.animateTo(geoPoint);
    mc.setZoom(17);
    geoPoint=g;
    mapView.setOnTouchListener(touch);

    mapView.postInvalidate();
    }

private OnTouchListener touch = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        mapView=(MapView) findViewById(R.id.MapView);

        if (event.getAction() == 1) {                
           GeoPoint  geoPoint = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
           g=geoPoint;
                Toast.makeText(getBaseContext(), 
                    geoPoint.getLatitudeE6() / 1E6 + "," + 
                    geoPoint.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();

        }

        return true;
    }
};

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


}

And I created a custom overlay:

package com.example.googledemo4;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.webkit.WebView.FindListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapOverlay4 extends Overlay{

GeoPoint p ;
MapView mapView;
Context context;

public MapOverlay4(GeoPoint geoPoint, MapView mapView, Context context){

    this.mapView=mapView;
    this.p=geoPoint;
    this.context=context;
}
public void draw(Canvas canvas, MapView mapView, boolean flag) {
    // TODO Auto-generated method stub
    super.draw(canvas, mapView, flag);
      //---translate the GeoPoint to screen pixels---
    mapView=this.mapView;
    Point screenPts = new Point();
    mapView.getProjection().toPixels(p, screenPts);

    //---add the marker---
    Bitmap bmp = BitmapFactory.decodeResource(
        this.context.getResources(), R.drawable.ic_launcher);            
    canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         


}


}

This code draws the icon at the first time because I initialized the geopoint to some lat and long. But after when the map is touched, just nothing happens. I thought whenever the map touched the geopoint would change and that changed geopoint would get supplied to the overlay.

Regards

Was it helpful?

Solution

There is no call similar to overlays.add(mapOverlay) with the new Geopoint within the onTouchListener . Doing that must help you to I suppose.

OTHER TIPS

try to use itemized overlay to draw image and to draw at certain point override the OntouchEvent Method and pass it the lat and long and you will get Image on the touched location if you need some example u can see here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top