参考
http://blog.csdn.net/kesenhoo/article/details/6566388
http://blog.sina.com.cn/s/blog_74c22b210100sfix.html
事实证明问题就出在位置服务获取方式上,改成使用网络方式后可以在真机上获取到当前位置。
我的代码如下:
public class Activity01 extends MapActivity {
public MapController mapController;
public MyLocationOverlay myPosition;
public MapView myMapView;
private static final int ZOOM_IN = Menu.FIRST;
private static final int ZOOM_OUT = Menu.FIRST + 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得LocationManager实例
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
myMapView = (MapView) findViewById(R.id.MapView01);
// 取得MapController实例,控制地图
mapController = myMapView.getController();
// 设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
// 设置缩放控制,这里我们自己实现缩放菜单
myMapView.displayZoomControls(false);
// 设置使用MyLocationOverlay来绘图
mapController.setZoom(17);
myPosition = new MyLocationOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(myPosition);
// 设置Criteria(服务商)的信息
Criteria criteria = new Criteria();
// 提供服务的精度标准
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//不需要高度信息
criteria.setAltitudeRequired(false);
//不需要方位信息
criteria.setBearingRequired(false);
//不允许产生费用
criteria.setCostAllowed(true);
//消耗电力为低
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 取得效果最好的criteria
//String provider = locationManager.getBestProvider(criteria, true);
String provider = locationManager.NETWORK_PROVIDER;
if (provider == null) {
Log.e("provider", "null");
} else {
Log.e("provider", provider);
}
// 得到坐标的相关的信息
Location location = locationManager.getLastKnownLocation(provider);
/*//得到地址编码,用于后面查找当前地址名称
mGeocoder = new Geocoder(this,Locale.getDefault());*/
// 更新坐标
updateWithNewLocation(location);
// 注册一个周期性的更新,3000ms更新一次
// locationListener用来监听定位信息的改变
locationManager.requestLocationUpdates(provider, 6000, 100,
locationListener);
}
//更新定位
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText = (TextView) findViewById(R.id.TextView01);
String addressString = "没有找到地址\n";
if (location != null) {
// 为绘制标志的类设置坐标
Log.e("location", "not null");
myPosition.setLocation(location);
// 取得经度和纬度
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
// 将其转换为int型
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
// 定位到指定坐标
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "经度:" + lat + "\n纬度:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
// 根据地理环境来确定编码
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
// 取得地址相关的一些信息,经度,纬度
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString = sb.toString();
}
} catch (IOException e) {
}
} else {
latLongString = "没有找到坐标.\n";
Log.e("location", "null");
}
// 显示
myLocationText.setText("你当前的坐标如下:\n" + latLongString + "\n"
+ addressString);
}
private final LocationListener locationListener = new LocationListener() {
// 当坐标改变时触发此函数
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
// Provider禁用时触发此函数,比如GPS被关闭
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
// Provider启用时触发此函数,比如GPS被打开
public void onProviderEnabled(String provider) {
}
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
protected boolean isRouteDisplayed() {
return false;
}
// 为应用程序添加菜单
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ZOOM_IN, Menu.NONE, "放大");
menu.add(0, ZOOM_OUT, Menu.NONE, "缩小");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case (ZOOM_IN):
// 放大
mapController.zoomIn();
return true;
case (ZOOM_OUT):
// 缩小
mapController.zoomOut();
return true;
}
return true;
}
class MyLocationOverlay extends Overlay {
Location mLocation;
// 在更新坐标时,设置该坐标,以便画图
public void setLocation(Location location) {
mLocation = location;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
// 将经纬度转换成实际屏幕坐标
int x;
int y;
if (mLocation == null) {
x = 0;
y = 0;
} else {
x = (int) (mLocation.getLatitude() * 1E6);
y = (int) (mLocation.getLongitude() * 1E6);
}
GeoPoint tmpGeoPoint = new GeoPoint(x, y);
mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.home);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y,
paint);
return true;
}
}
}
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"
>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.google.android.maps.MapView
android:id="@+id/MapView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0TKF9jqsJq6tPYSlW80pFqyvebDuC7Y97bD0j5w"/>
</LinearLayout>
注意我的
manifestapiKey为0TKF9jqsJq6tPYSlW80pFqyvebDuC7Y97bD0j5w。方法通过查找自己的MD5: 78:7D:AB:4D:65:06:0D:EB:EB:00:DB:BB:92:08:CF:D4 (我的)
manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-sdk android:minSdkVersion="5" />