做了一个很简单的demo。打开GPS,通过模拟器发送坐标,使地图以接受到的坐标为中心。
但是GPS一直无法启动,大家能帮我看看,在你们的电脑上可以启动吗?
这是运行在2.2上面的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />    <com.google.android.maps.MapView
        android:id="@+id/myMapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0Ao5sdg8IRBiavp_y3M240_3joxmkzOSM0ENQuw" 
        android:clickable="true" /></LinearLayout><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chenjb.maptest"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" />        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application></manifest>package com.chenjb.maptest;import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;public class MainActivity extends MapActivity {
GeoPoint centerGeoPoint = null; MapController mc;
LocationManager locMngr; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 初始化地图
final MapView mv = (MapView) findViewById(R.id.myMapView);
mv.setBuiltInZoomControls(true);// 设置地图上显示缩放控制条
mc = mv.getController();// 获得MapView的地图控制器
mc.setZoom(14);// 设置地图缩放比例 locMngr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
criteria.setAltitudeRequired(false);// 不要求海拔
criteria.setBearingRequired(false);// 不要求方位
criteria.setCostAllowed(true);// 允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
// 从可用的位置提供器中,匹配以上标准的最佳提供器
String provider = locMngr.getBestProvider(criteria, true);
System.out.println(provider);
// // 获得最后一次变化的位置
// Location location = locMngr.getLastKnownLocation(provider);
// if (location == null) {
// System.out.println("location为null");
// } else {
// System.out
// .println(location.getLatitude() + location.getLongitude());
// } // 监听位置变化,2秒一次,距离2米以上
locMngr.requestLocationUpdates(provider, 2000, 2, locationListener); } private final LocationListener locationListener = new LocationListener() { @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
} @Override
public void onProviderEnabled(String provider) {
} @Override
public void onProviderDisabled(String provider) {
} @Override
public void onLocationChanged(Location location) {
GeoPoint gp = getGeoByLocation(location);
mc.animateTo(gp);
}
}; // 通过Location获取GeoPoint
public GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
double geoLatitude = location.getLatitude() * 1E6;
double geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
return gp;
} @Override
protected boolean isRouteDisplayed() {
return false;
}
}