Gautam
Gautam

Reputation: 7958

Select an Area in MapView Android

Is is possible to show a map-view in android an allow the user to select an area for example a country or state ?

Clarification :

I want to let users select a country from the map view , How is that possible ?

I don't care about whether the selected country is highlighted or not , I only care about retrieving which country was selected / touched/ marked by the user

Upvotes: 0

Views: 1506

Answers (2)

Adeel Pervaiz
Adeel Pervaiz

Reputation: 1356

There's a method to get lat, lon from map how to get lat and long on touch event from google map?

After that you can call Google Geo Code API to convert this lat,lon to physical address,

i.e.

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

The Above link will return JSON

{
  "status": "OK",
  "results": [ {
    "types": street_address,
    "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
    "address_components": [ {
      "long_name": "275-291",
      "short_name": "275-291",
      "types": street_number
    }, {
      "long_name": "Bedford Ave",
      "short_name": "Bedford Ave",
      "types": route
    }, {
      "long_name": "New York",
      "short_name": "New York",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Brooklyn",
      "short_name": "Brooklyn",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Kings",
      "short_name": "Kings",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "New York",
      "short_name": "NY",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "11211",
      "short_name": "11211",
      "types": postal_code
    } ],
    "geometry": {
      "location": {
        "lat": 40.7142298,
        "lng": -73.9614669
      },
      "location_type": "RANGE_INTERPOLATED",
      "viewport": {
        "southwest": {
          "lat": 40.7110822,
          "lng": -73.9646145
        },
        "northeast": {
          "lat": 40.7173774,
          "lng": -73.9583193
        }
      }
    }
  },

  ... Additional results[] ...

Now you can parse JSON and get country name city name locality, state etc etc.

Or other option is to get just country code.

http://ws.geonames.org/countryCode?lat=25.03&lng=67.2

this will return country code PK = Pakistan you can either get the country code list, or

Upvotes: 3

Braj
Braj

Reputation: 2160

One solution I know is that you can mark particular area by map pins and fill that area with some color by extending OverLay class in Map like below. Hope this is useful.

public class MapOverlay extends Overlay
{
    private GeoPoint new_geopoint = null;

    public MapOverlay(GeoPoint geoPoint) 
    {

    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) 
    {
        super.draw(canvas, mapView, shadow);

        Paint mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setStyle(Style.FILL_AND_STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setAlpha(9);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        Path path = new Path();

        Projection projection = mapView.getProjection();

        for(int j = 0; j < geoArrayist.size(); j++) 
        {
            Iterator<GeoPoint> it = geoArrayist.iterator();
            while(it.hasNext()) 
            {
                GeoPoint arrayListGeoPoint = it.next();

                Point currentScreenPoint = new Point();
                projection.toPixels(arrayListGeoPoint, currentScreenPoint);

                if(j == 0)
                    path.moveTo(currentScreenPoint.x, currentScreenPoint.y); 
                else
                    path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
            }                 
        }
       // old_geopoint = new_geopoint;
        canvas.drawPath(path, mPaint);
    }       
}

Upvotes: 1

Related Questions