位置管理器:LocationManager

定位服务

配置Google API SDK

  配置Google API SDK,新建一个适合Google SDK的虚拟机,选择

Google APIs(Google Inc.)-API Level 10

  使用Google API启动的虚拟机存在一个Maps的机制。

  新建项目的时候选择Google APIs(Google Inc.)(API 10)

 

位置管理器:LocationManager_第1张图片

 

 

 

新建一个地图项目。

 

 

在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"

  android:background="#3399ff">

  <TextView

     android:id="@+id/msg"

     android:layout_marginTop="8dp"

     android:layout_marginLeft="80dp"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content"

     android:textColor="#ffffff"

     android:text="等待接收位置信息..." />

</LinearLayout>

 

 

 

 

在MyGPSDemo.java中:

 

package com.li.gprs;

 

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.TextView;

 

public class MyGPSDemo extends Activity {

  private TextView msg = null;

  private LocationManager locationManager = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.msg = (TextView) super.findViewById(R.id.msg);

     this.locationManager = (LocationManager) super

         .getSystemService(Context.LOCATION_SERVICE);

     this.locationManager.requestLocationUpdates(

         LocationManager.GPS_PROVIDER, 1000, 1,

         new LocationListenerImpl());

  }

 

  private class LocationListenerImpl implements LocationListener {

 

     public void onLocationChanged(Location location) { // 位置改变

       MyGPSDemo.this.msg.setText("用户位置发生改变,新的数据:\n" + "经度:"

            + location.getLongitude() + "\n" + "纬度:"

            + location.getLatitude() + "\n" + "数据准确度:"

            + location.getAccuracy() + "\n" + "时间:"

            + location.getTime() + "\n" + "速度:" + location.getSpeed()

            + "\n" + "方位:" + location.getBearing());

     }

 

     public void onProviderDisabled(String provider) {

 

     }

 

     public void onProviderEnabled(String provider) {

 

     }

 

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

 

     }

 

  }

}

 

 

 

在AndroidManifest.xml中配置权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.gprs"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

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

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

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyGPSDemo"

            android:label="@string/title_activity_my_gpsdemo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

你可能感兴趣的:(位置管理器:LocationManager)