Pages

Friday, 2 March 2012

Current Location on map in Android

Step1:Create new android project.
File/new project/android project/showmapactivity.

Step2:Create main.xml file with map api key.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    >


<TextView
    android:id="@+id/text"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"/>
   
<com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="your map api key..." />
   
<LinearLayout android:id="@+id/zoom"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</RelativeLayout>


Step3:Edit showmapAcivity class.
package com.android;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.RelativeLayout;
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 ShowMapActivity extends MapActivity {

    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    private Location location;
    private MapOverlay mapOverlay;
    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.add);           
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);        
            return true;
        }
    }
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main); // bind the layout to the activity

        // create a map view
        RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(5); // Zoon 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if(LocationManager.GPS_PROVIDER != null)
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 10, new GeoUpdateHandler());
        else
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 10, new GeoUpdateHandler());
       

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
           
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            p = new GeoPoint(lat, lng);
            mapController.animateTo(p);
            //    mapController.setCenter(point);
            mapOverlay = new MapOverlay();
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);       
   
            mapView.invalidate();
            //updateWithNewLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
            //updateWithNewLocation(null);
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

}

Step4:Edit androidmanifest.xml file and add uses permission.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Step5:Now save your project and run in emulator.

Step6:You'll get an output like as...

1 comment:

Kate Winslt said...

create personal, professional, community maps and share on social network, blogs and sites on community walk

Post a Comment