One of the major functionality provided by the smartphone is GPS [Global Positioning System] enable. I never realized that interacting with GPS functionality is so easy until I worked with it. This article is a way to help people realize the same.This article is targeted to a beginner who wants to implement GPS functionality and work with Google Maps API as well.
" Challenges During getting location into Google maps"
- First Question arise in mind is that When we want to get current location in our apps then how our phone find out Geo Co-Ordinates [Latitude and Longitude] values?
- With respect to this question as per my knowledge phone gets current location using different quipped devices and methods like.... GPS, USING NETWORK PROVIDER, AND OFF COURSE USING INTERNET CONNECTION.
- Second Question arise in mind is that How it all be possible?
- With respect to this question, we have basic knowledge of android sdk, how to create simple application in android and have conceptually clear in different component of project like Activity, Intents, Views, XML Viewer, Activity Life cycle, basic knowledge of AndroidManifest.xml, Emulator, basic knowledge of Eclipse, and last but not least is Android Emulator, and DDMS, Because without it we might be not able to build this application.
- For rest of answer of your questions please go through this tutorial and if you have any doubt then comment it at last of article...
[1] Getting google map api key (fist get fingerprint key values and after that use it for getting map api key.)
[2] After getting google map api key used it into your android application for authorizing your app. for that you have to put whole key into xml file of your view.
Now some Questions are arises in mind like How to get map api key? How to use it ? So, the answer is given below.
We create new certificate using keytool.exe available in your java\bin directory, into your command prompt type following command...
keytool -list -alias androiddebugkey -keystore "your path put here\debug.keystore" -storepass android -keypass android
In above command we used one file name is "debug.keystore", is used for using app in debug mode and we get md5 Google map api key and whole debugging structure stored or located into your own file. after running above command you will get your fingerprint md5 Google map key. after that Go to google api signup page and use your fingerprint key for getting apikey.
After that Create New Android Project, Create new AVD with target mode = Google APIs
after that first you have to setup your geo location in emulator, because if you tested your app in emulator then it will not directly get current location, we have to supply it explicitly.
for that open your cmd, type "telnet localhost <your emulator port no like... 5554>"
and now set your current location into your phones GPS by applying command
geo fix <Latitude value> <Longitude value>
Now first step is open your main.xml file and put code given below
<?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" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="Your APIKEY goes here..."
/>
</RelativeLayout>
<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" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="Your APIKEY goes here..."
/>
</RelativeLayout>
Second step is to update your AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gaurang.android.MapView"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<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" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.gaurang.android.MapsViews.MapsView"
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:required="true" android:name="com.google.android.maps">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gaurang.android.MapView"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<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" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.gaurang.android.MapsViews.MapsView"
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:required="true" android:name="com.google.android.maps">
</uses-library>
</application>
</manifest>
</application>
</manifest>
Third step is to code for our application so open your Activity.java file
package com.gaurang.android.MapsViews;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.gaurang.android.MapView.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapsView extends MapActivity {
/** Called when the activity is first created. */
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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(16); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new GeoUpdateHandler());
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location !=null){
//int lat = (int) (location.getLatitude() * 1E6);
//int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_LONG).show();
Log.i("GPS Cordinates", "LAT= "+ location.getLatitude() + "LONG= "+ location.getLongitude());
//mapController.animateTo(point);
mapController.setCenter(point);
mapController.setZoom(16);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.gaurang.android.MapView.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapsView extends MapActivity {
/** Called when the activity is first created. */
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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(16); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new GeoUpdateHandler());
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location !=null){
//int lat = (int) (location.getLatitude() * 1E6);
//int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_LONG).show();
Log.i("GPS Cordinates", "LAT= "+ location.getLatitude() + "LONG= "+ location.getLongitude());
//mapController.animateTo(point);
mapController.setCenter(point);
mapController.setZoom(16);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
Now test your app into your emulator and see the map of your current(nearest) location. You also required to do change in above code as per your settings and packages files and also as per your project and activity name.
The class "LocationManager" provides access to the location.You can register a "LocationListener" with the "LocationManager" and will receive periodic updates about the geoposition. The class "LocationProvider" is the superclass of the different location providers which deliver the information about the current location.
Each and every time when user changes location of phone then automatically fires public void onLocationChanged(Location location) method and what we written into this whole runs successfully. onLocationChanged(Location location) method is given into the class LocationListner and we used this method as a method Override concepts.
So i think from my side i explained you with short content but after reading this i wish that it will be useful to various beginner developer like me so
"Keep sharing your knowledge and also use your knowledge"
Thanks to all who read this content.
No comments:
Post a Comment