首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

android获取不到目前经纬度

2012-09-24 
android获取不到当前经纬度Java codepackage com.gpdiimport com.autonavi.mapapi.GeoPointimport com.a

android获取不到当前经纬度

Java code
package com.gpdi;  import com.autonavi.mapapi.GeoPoint;import com.autonavi.mapapi.MapActivity;import com.autonavi.mapapi.MapController;import com.autonavi.mapapi.MapView;import com.autonavi.mapapi.MyLocationOverlay;  import android.content.Context;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;@SuppressWarnings("unused")public class MapTestActivity  extends MapActivity {         MapView mMapView;     MapController mMapController;     GeoPoint point;    private double latitude=0.0;    private double longitude =0.0;         @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                  LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);            if(location != null){                latitude = location.getLatitude();                longitude = location.getLongitude();                }        }else{            LocationListener locationListener = new LocationListener() {                                // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数                @Override                public void onStatusChanged(String provider, int status, Bundle extras) {                                    }                                // Provider被enable时触发此函数,比如GPS被打开                @Override                public void onProviderEnabled(String provider) {                                    }                 // Provider被disable时触发此函数,比如GPS被关闭                 @Override                public void onProviderDisabled(String provider) {                                    }                                //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发                 @Override                public void onLocationChanged(Location location) {                    if (location != null) {                           Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());                       }                }            };            //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 0,locationListener);             Criteria crit = new Criteria();             crit.setAccuracy(Criteria.ACCURACY_FINE);            String provider = locationManager.getBestProvider(crit, true);             Location location = locationManager.getLastKnownLocation(provider);             //Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);               if(location != null){                   latitude = location.getLatitude(); //经度                   longitude = location.getLongitude(); //纬度             }         }        mMapView = (MapView) findViewById(R.id.atmapsView);         mMapView.setBuiltInZoomControls(true); //设置吭用内置的缩放控件         mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放         point = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6)); //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)        mMapController.setCenter(point); //设置地图中心点         mMapController.setZoom(12); //设置地图zoom级别    }}

获取的location全为null
权限已设置:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>


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

[解决办法]
兄弟,解决了吗,我是wifi和基站定位,onLocatioinChange()总也不触发, 也没报错,这是怎么回事儿啊?
[解决办法]
楼主解决了吗,我最近也遇到这种问题,QQ:244276818 线上交流交流。。。
[解决办法]
我的解决了
[解决办法]
我也遇到同样的问题了,谁能帮帮我,谢谢了!邮箱:408647430@qq.com

热点排行