位置管理器: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)

 



 

 

 

新建一个地图项目。

 

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值