1

I am working with an activity that needs to know a location that the user enters in. Ideally, the user will press a button in my activity, which will then send the user into a maps activity where they can place a pin, then return to my activity with the pin location.

Is there an easy way to do this in Android or will I have to make my own MapView?

Alexis
  • 23,545
  • 19
  • 104
  • 143

2 Answers2

2

You will have to make your own MapView and then use the onTouchListener() for it to detect a click on the map and based off the click location you will be able to get the geopoint using this code:

class HelloGoogleMaps extends MapActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
 List<Overlays> mapOverlays = map.getOverlays();
 //This is going to be your pointer (you can use whatever image you want as your indicator
 final Drawable drawable = this.getResources().getDrawable(R.drawable.indicator);
 map.setOnTouchListener(new View.OnTouchListener() {

 @Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP)
    {
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, HelloGoogleMaps.this);
    GeoPoint p = mapView.getProjection().fromPixels(
        (int) event.getX(), (int) event.getY());
    String lat = Double.toString(p.getLatitudeE6() / (Math.pow(10,6)));
    String lon = Double.toString(p.getLongitudeE6() / (Math.pow(10,6)));
    OverlayItem overlayitem = new OverlayItem(p, "Title text", "Body Text");
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
    map.postInvalidate();
    Intent i = new Intent();
    i.putExtra("latitude", Double.parseDouble(lat));
    i.putExtra("longitude", Double.parseDouble(lon));
    this.setResult(RESULT_OK, i);
    finish();
    return true;
    }
}
});
}

Regarding the HelloItemizedOverlay class mentioned above, refer to this Google Maps Tutorial for the exact implementation. This will create markers wherever you click on the map and also return the result (latitude, longitude) back to the original activity from where you called it.

A Random Android Dev
  • 2,691
  • 1
  • 18
  • 17
0

I would be further difficult to make your "own" mapview. I think you mean to use ItemizedOverlay class. You can put pins to mapview by overriding onTap() methods of this class.