Saturday 8 December 2012

Android-Get the current location using GPS and draw it in Maps



 This is a simple android application to find the current location and draw it in Mapview.
First you have get the Google Api keyhttps://developers.google.com/android/maps-api-signup
After getting the Api key try the below code..
Showmap.java

package com.example.showmap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


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 android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class Showmap extends MapActivity {
       private MapView mapView;
       private MapController mapController;
    private LocationManager locationManager;
    private LocationListener locationListener;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showmap);
       
   LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
       
        locationListener = new GPSLocationListener();
       
        locationManager.requestLocationUpdates
        ( LocationManager.GPS_PROVIDER,  0, 0,   locationListener);
       
        mapView = (MapView) findViewById(R.id.mapView);
       
        // enable Street view by default
        mapView.setStreetView(true);
       
        // enable to show Satellite view
        // mapView.setSatellite(true);
       
        // enable to show Traffic on map
        // mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);   
        mapController = mapView.getController();
        mapController.setZoom(16);
    }
      
       protected boolean isRouteDisplayed() {
        return false;
    }
   
    private class GPSLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                GeoPoint point = new GeoPoint(
                        (int) (location.getLatitude() * 1E6),
                        (int) (location.getLongitude() * 1E6));
               
//                Toast.makeText(getBaseContext(),
//                        "Latitude: " + location.getLatitude() +
//                        " Longitude: " + location.getLongitude(),
//                        Toast.LENGTH_SHORT).show();
             
                mapController.animateTo(point);
                mapController.setZoom(16);
               
                // add marker
                MapOverlay mapOverlay = new MapOverlay();
                     mapOverlay.setPointToDraw(point);
                     List<Overlay> listOfOverlays = mapView.getOverlays();
                     listOfOverlays.clear();
                     listOfOverlays.add(mapOverlay);
                String address = ConvertPointToLocation(point);
             
                Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
                mapView.invalidate();
            }
        }
       
        public String ConvertPointToLocation(GeoPoint point)
        {  
              String address = "";
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                     point.getLatitudeE6()  / 1E6,
                    point.getLongitudeE6() / 1E6, 1);
        
                      if (addresses.size() > 0) {
                          for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                            address += addresses.get(0).getAddressLine(index) + " ";
                          Log.i(address, address);
                      }
                     
            }
            catch (IOException e) {               
                e.printStackTrace();
            }  
           
            return address;
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
             
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
   
    class MapOverlay extends Overlay
    {
       private GeoPoint pointToDraw;

       public void setPointToDraw(GeoPoint point) {
              pointToDraw = point;
       }

       public GeoPoint getPointToDraw()
       {
              return pointToDraw;
             
       }
      
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                  

            // convert point to pixels
            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);
           
            // add marker
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image       
            return true;
        }
    }
      
}




 main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0YxUAcdaf-88HoJUjDgQS29r9ZAoGKOII8zCXsg"
        android:clickable="true"
        android:enabled="true" />
    
</RelativeLayout>

 Add  
  <uses-library android:name="com.google.android.maps" />   in Android manifest
   and give permission 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
 
Output
 


1 comment:

  1. How to login to your casino, find out how to - JTM Hub
    To get started on the go with the 포천 출장마사지 click of the 태백 출장마사지 link you can open the Login account. 고양 출장마사지 Go 경산 출장샵 to the Home Page on the 청주 출장샵 home page of your desktop or

    ReplyDelete