Location based service : Location « Hardware « Android






Location based service

 
package app.test;


import android.os.Bundle;
import android.location.LocationManager;
import android.view.View;
import android.widget.TextView;
import android.content.Context;
import android.widget.Button;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;
import com.google.android.maps.MapController;



public class Test extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        final MapView myMap = (MapView) findViewById(R.id.myMap);
        final MapController myMapController = myMap.getController();
        final Button zoomIn = (Button) findViewById(R.id.buttonZoomIn);
        zoomIn.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v){
             ZoomIn(myMap,myMapController);
           }});
        final Button zoomOut = (Button) findViewById(R.id.buttonZoomOut);
        zoomOut.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v){
             ZoomOut(myMap,myMapController);
           }});
        final Button gpsButton = (Button) findViewById(R.id.gpsButton);
       gpsButton.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v){
            LoadProviders(myMap,myMapController);
          }});
       final Button viewMap = (Button) findViewById(R.id.buttonMapView);
       viewMap.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v){
            ShowMap(myMap,myMapController);
          }});
       final Button viewSat = (Button) findViewById(R.id.buttonSatView);
       viewSat.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v){
            ShowSat(myMap,myMapController);
          }});
             
    }

    public void LoadProviders(MapView mv, MapController mc){
      TextView latText = (TextView) findViewById(R.id.latText);
      TextView lngText = (TextView) findViewById(R.id.lngText);
      LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      Double latPoint = myManager.getCurrentLocation("gps").getLatitude()*1E6;
      Double lngPoint = myManager.getCurrentLocation("gps").getLongitude()*1E6;
      latText.setText(latPoint.toString());
      lngText.setText(lngPoint.toString());
       Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());
       mc.centerMapTo(myLocation, false);
       mc.zoomTo(9);
       mv = null;
    }
    public void ZoomIn(MapView mv, MapController mc){
      if(mv.getZoomLevel()!=21){
      mc.zoomTo(mv.getZoomLevel()+ 1);
      }
    }
    public void ZoomOut(MapView mv, MapController mc){
      if(mv.getZoomLevel()!=1){
          mc.zoomTo(mv.getZoomLevel()- 1);
          }
    }
    public void ShowMap(MapView mv, MapController mc){
        if (mv.isSatellite()){
          mv.toggleSatellite();
        }
    }
    public void ShowSat(MapView mv, MapController mc){
    if (!mv.isSatellite()){
      mv.toggleSatellite();
    }
    }
    }




//main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
  android:id="@+id/gpsButton"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Where Am I"
    />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    > 
<TextView
  android:id="@+id/latLabel"
  android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Latitude: "
    /> 
<TextView
  android:id="@+id/latText"
  android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />
 </LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >   
 <TextView
  android:id="@+id/lngLabel"
  android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Longitude: "
    />   
 <TextView
  android:id="@+id/lngText"
  android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <view class="com.google.android.maps.MapView"
        android:id="@+id/myMap" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
          <Button android:id="@+id/buttonZoomIn"
            style="?android:attr/buttonStyleSmall"
            android:text="+"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
          <Button android:id="@+id/buttonMapView"
            style="?android:attr/buttonStyleSmall"
            android:text="Map"
            android:layout_alignRight="@+id/myMap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
          <Button android:id="@+id/buttonSatView"
            style="?android:attr/buttonStyleSmall"
            android:text="Sat"
            android:layout_alignRight="@+id/myMap"
            android:layout_alignBottom="@+id/myMap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
          <Button android:id="@+id/buttonZoomOut"
            style="?android:attr/buttonStyleSmall"
            android:text="-"
            android:layout_alignBottom="@+id/myMap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />          
</RelativeLayout>  


</LinearLayout>

   
  








Related examples in the same category

1.Location service and LocationManager
2.Location service
3.Using LocationManager
4.My location
5.Display GEO location
6.Using location service for the weather
7.Using Intent to go to a geo location
8.My location and Google Map
9.Custom Location Overlay
10.Get my location
11.Geo location and Google Map
12.Location Tracking
13.A light pool of objects that can be resused to avoid allocation.
14.extends android.location.Location
15.Geo Location Util
16.C:\Java_Dev\WEB\dev\android\weatherforecastsystem-read-only\com\hci\pwf\LocationUtil.java
17.upload Data with Geo location
18.Copy a file from one location to another.
19.LocationManager.GPS_PROVIDER
20.Location util