Android编写gps定位在模拟器上为什么总是无法获取地理位置

veronica001 发布于 2012/06/02 20:29
阅读 4K+
收藏 1

Serverless 架构就不要服务器了?>>>

package com.hsh.Activity;

import java.net.ContentHandler;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class GpsActivity extends Activity {
 private static final String TAG = "GPSTestActivity";
 private Location preLocation;
 private long preTime;
 private static final int step = 2000; // msecs
 private static final String Content = null;
 private LocationManager locationManager;
 private boolean status = false;
 private Button btn;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Log.i(TAG, "onCreate(Bundle savedInstanceState)");
  btn = (Button) findViewById(R.id.beginBtn);
  btn.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    if (!status) {
     status = true;
     startLoactionService();
     btn.setText("点击这里停止");
    } else {
     status = false;
     stopLocationService();
     btn.setText("点击这里开始");
    }
   }
  });
 }
 public void startLoactionService() {
  String serviceName = Context.LOCATION_SERVICE;
  
  //LocationManager locationManager;///
  locationManager = (LocationManager) getSystemService(serviceName);
  
  
  // String provider = LocationManager.GPS_PROVIDER;///
  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 = locationManager.getBestProvider(criteria, true);
  Location location = locationManager.getLastKnownLocation(provider);
  
  updateWithNewLocation(location);///
  preTime = System.currentTimeMillis();
  updateWithNewLocation(location);
  locationManager.requestLocationUpdates(provider, step, 0,
  locationListener);                   //注册一个周期性的位置更新。这句代码告诉系统,我们需要从GPS获取位置信息,并且是每隔1000ms更新一次,并且不考虑位置的变化。最后一个参数是LocationListener的一个引用,我们必须要实现这个类

 }
 public void stopLocationService() {
  if (locationManager != null) {
   locationManager.removeUpdates(locationListener);
  }
 }
 private final LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {        //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
    // log it when the location changes

   Log.i(TAG, "onLocationChanged(Location location)");
   //Log.i("SuperMap","LocationChange:Lat"+location.getLatitude()+"Lng:"+location.getLongitude());///
   
   updateWithNewLocation(location);
  }
  public void onProviderDisabled(String provider) {
   updateWithNewLocation(null);    // Provider被disable时触发此函数,比如GPS被关闭

  }
  public void onProviderEnabled(String provider) {          //  Provider被enable时触发此函数,比如GPS被打开

  }
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数

  }
 };
 


 private void updateWithNewLocation(Location location) {
  Log.i(TAG, "updateWithNewLocation(Location location)");
  String latLongString;
  TextView myLocationText;
  myLocationText = (TextView) findViewById(R.id.result);
  
  //myLocationText=(TextView)findViewById(R.id.myLocationText);///
  if (location != null) {
   double lat = location.getLatitude();        //经度
   double lng = location.getLongitude();        //纬度
   
   long subTime = (System.currentTimeMillis() - preTime) / 1000;
   float v = (subTime == 0 || preLocation == null) ? 0 : (preLocation
     .distanceTo(location) / subTime);
   latLongString = "纬度:" + lat + " 经度:" + lng + " 速度:" + v + " m/s , "
     + v * 3.6 + " km/h";
   preLocation = location;
   preTime = System.currentTimeMillis();
   
  } else {
   latLongString = "无法获取地理信息";
  }
  myLocationText.setText(System.currentTimeMillis() + ": "
    + latLongString);
 }
 
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

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"
    >
    <Button
 android:id="@+id/beginBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="start"
    />
<TextView 
 android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="data"
    />
</LinearLayout>

 

每次运行都是无法获取地理位置

加载中
返回顶部
顶部