Sunday, 14 April 2013

Adding Overlay on Map in Android


By default Google map open in United State .In this project we will make an application which will able to open the Google map at a predefined location and we will use an overlay to identify that particular location on the map.

1)create a new project named as AddingOverlay.

2)In the main.xml file populate as:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0NcXvM0uInMbv7LHmKeRwbksW8Z4u3bYMWxCrmQ" />

3)copy an image named as pushpin in drawable-mdpi.

3)In the java file match as:
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
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;

public class GoogleWithZoomActivity extends MapActivity{
      
       MapView mapView;
       MapController mc;
       GeoPoint p;
      
       class MapOverlay extends com.google.android.maps.Overlay
       {
       @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.pushpin);
                     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
                     return true;
                     }
                     }
      
      
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mapView = (MapView) findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(true);
       mapView.setStreetView(true);
        mc = mapView.getController();
       String coordinates[] = {"28.613459", "77.107959"};
        double lat = Double.parseDouble(coordinates[0]);// parseing in double
        double lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint( (int) (lat * 1E6),(int) (lng * 1E6));
        mc.animateTo(p);
        mc.setZoom(13);
      //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
       
        }
   
       @Override
       protected boolean isRouteDisplayed() {//This is a override Method they was displaying Routes on the Map
              // TODO Auto-generated method stub
              return false;
       }
      
}
// Manifest File

4)In the AndroidMenifest file add the permissions as:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.googlemapwithzoom"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
// Without Permission they was not working ..so to use the map to allow the Internet Permission
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

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

    </application>
</manifest>


0 comments:

Post a Comment

 

© 2011 Reach 2 Android - Tutorials on Android and PHP - Designed by Mukund | ToS | Privacy Policy | Sitemap

About Us | Contact Us | Write For Us