Google地图开发

配置Google API SDK

如果要想进行Google Map或者说是定位服务的开发,那么肯定需要下载一个新的SDK的支持。

1、点击Android SDK Manager,下载SDK。

2、直接配置已经下载好的SDK到项目中。

配置完Google API SDK之后,那么下面就需要建立一个适合于Google的SDK的虚拟机。

LocationManager的使用

1、主要逻辑部分

/**
 * 
 * @author Harvey
 * 
 * 
 */
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); // 默认布局管理器

        msg = (TextView) super.findViewById(R.id.msgTv); // 获得组件
        locationManager = (LocationManager) super.getSystemService(Context.LOCATION_SERVICE); // 取得位置服务
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, // GPS定位提供者
                1000, // 时间间隔设置为1秒
                1, // 位置间隔设置为1米
                new MyLocationListener()); // 设置位置监听
    }

    private class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location)
        {
            // 设备位置发生改变时触发
            msg.setText("用户位置发生改变,新的位置数据:\n" + "经度:" + location.getLongitude() + "\n" + "纬度:"
                    + location.getLatitude() + "\n" + "数据精确度:" + location.getAccuracy() + "\n"
                    + "时间:" + location.getTime() + "\n" + "速度:" + location.getSpeed() + "\n"
                    + "方位:" + location.getBearing()); // 设置文本信息
        }

        @Override
        public void onProviderDisabled(String provider)
        {
            // 当数据提供者关闭时触发
        }

        @Override
        public void onProviderEnabled(String provider)
        {
            // 当数据提供者启用时触发
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            // 当位置信息状态更新时触发
        }
    }
}

2、布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/msgTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/get_msg" />

</LinearLayout>

3、需要在AndroidManifest.xml文件中添加权限

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

 

 

posted on 2013-08-18 23:41  Harvey Ren  阅读(1442)  评论(0编辑  收藏  举报

导航