1 Replies - 5370 Views - Last Post: 19 May 2012 - 10:14 PM

#1 johnOwl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-May 12

add marker on touch at google maps

Posted 15 May 2012 - 03:43 PM

Hello every one
I'm new to android (about 2 months) and i'm trying to place markers on the map at the place where the user presses the screen

My code is here
package com.android.Maps;

import java.util.ArrayList;
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 com.google.android.maps.OverlayItem;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;


public class GoogleMapsActivity extends MapActivity {

	
	
	MapView mapView; 
	MapController mc;
	GeoPoint p;
	Canvas canvas;
	TextView lon ,lat;

	class MarkerOverlay extends Overlay{
	     private GeoPoint p; 
	     MotionEvent event;
	     
	     public MotionEvent GetEvent(){
	    	 return event;
	     }
	     
	     public MarkerOverlay(GeoPoint p){
	         this.p = p;
	     }

	     public boolean onTouchEvent(MotionEvent event, MapView mapView) {              
	         if (event.getAction() == 1) {                
	                 GeoPoint p = mapView.getProjection().fromPixels(
	                     (int) event.getX(),
	                     (int) event.getY());
	                     Toast.makeText(getBaseContext(),                             
	                         p.getLatitudeE6() / 1E6 + "," + 
	                         p.getLongitudeE6() /1E6 ,                             
	                         Toast.LENGTH_SHORT).show();
	          }                            
	             return false;
	         }
	     
	     
	     @Override
	     public boolean draw(Canvas canvas, MapView mapView, 
	            boolean shadow, long when){
	        super.draw(canvas, mapView, shadow);                   

	        //---translate the GeoPoint to screen pixels---
	        Point screenPts = new Point();
	        mapView.getProjection().toPixels(p, screenPts);

	        //---add the marker---
	        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);            
	        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
	        return true;
	     }
	 }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    
	    
	     mapView = (MapView) findViewById(R.id.mapView);
	     mapView.setSatellite(true);
	     mapView.setBuiltInZoomControls(true);
	     
	     mc = mapView.getController();
	     String coordinates[] = {"37.958473", "23.714446"};
	     double lat = Double.parseDouble(coordinates[0]);
	     double lng = Double.parseDouble(coordinates[1]);

	     
	     
	     p = new GeoPoint(
	         (int) (lat * 1E6), 
	         (int) (lng * 1E6));

	     mc.animateTo(p);
	     mc.setZoom(17); 
	     mapView.getOverlays().add(new MarkerOverlay(p));   
	     
	     mapView.postInvalidate();
	   
	}

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




when the user presses the screen it pop up a tost with the coordinates but i can't figure out how to use them

I tryied to put the mapView.getOverlays().add(new MarkerOverlay(p)); inside the onTouchEvent but if i do that it seems that the onTouchEvent does not work at all (It doesn't give toasts with coordinates nor the marker placed to map)

Also here is my Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.Maps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <uses-library android:name="com.google.android.maps"/>
        <activity
            android:name=".GoogleMapsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>




Every help is much appriciated
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: add marker on touch at google maps

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • Posts: 2,533
  • Joined: 29-July 11

Re: add marker on touch at google maps

Posted 19 May 2012 - 10:14 PM

Try here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1