1

我有新的华为手机 Ascend G330D,板载 4.0.4。问题是“网络”位置提供程序不起作用并且根本不返回任何坐标。

其他事实是 1. 应用程序是正确的 - 它可以在很多 android 设备上运行 2. gps 位置提供程序工作正常 3. Google 地图应用程序即使有网络位置(即没有 gps)也能完美运行!(我已经在 /system/app 中替换了 NetworkLocation.apk,之后 Google 应用程序(地图、本地)开始使用网络位置,最初它们都不起作用) 4. 没有任何 3rd 方应用程序(来自 Market)使用网络位置以及

由于地图不是开源的,我看不出这与使用谷歌 API 的应用程序有什么不同,但我猜谷歌地图可能会使用另一个地理定位来源(或授权密钥)......

有谁知道在哪里挖掘以使网络位置正常工作?

4

1 回答 1

0

您可以查看本教程以获取完整示例

获取当前位置坐标,城市名称

此外,这是我的工作代码,它与两者一起使用

  • GPS 提供商
  • 网络提供商

    public class MyLocationListener extends Service implements LocationListener {
    
    private static final String TAG = "MyLocationListener";
    
    private Context context = null;
    
    private Location location = null;
    private LocationManager locationManager = null;
    
    public static boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;
    
    public double latitude = 0.0;
    public double longitude = 0.0;
    public String location_address = null;
    
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    
    public MyLocationListener(Context ctx) {
        Log.v(TAG + ".MyLocationListener",
                "MyLocationListener constructor called");
        this.context = ctx;
        getLocationValue();
    }
    
    public Location getLocationValue() {
        Log.v(TAG + ".getLocationValue", "getLocationValue method called");
    
        try {
            locationManager = (LocationManager) context
                    .getSystemService(LOCATION_SERVICE);
    
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if (isGPSEnabled) {
    
                this.canGetLocation = true;
                Log.v(TAG + ".getLocationValue", "GPS provider enabled");
                // Toast.makeText(context, "Gps", 1).show();
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            Log.v(TAG, "Gps Co-ordinates are:" + latitude + " "
                                    + longitude);
    
                        }
                    }
                }
    
            } else if (isNetworkEnabled) {
    
                // Toast.makeText(context, "Net", 1).show();
                Log.v(TAG + ".getLocationValue", "Network provider enabled");
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.v(TAG, "Co-ordinates are: " + latitude + " "
                                + longitude);
    
                    }
                }
    
            } else {
                showSettingsAlert();
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return location;
    }
    
    /**
     * Stop using GPS listener Calling this function will stop using GPS in your
     * app
     * */
    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(MyLocationListener.this);
        }
    }
    
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }
        return latitude;
    }
    
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }
        return longitude;
    }
    
    /**
     * Function to check GPS/wifi enabled
     * 
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }
    
    /**
     * Function to show settings alert dialog On pressing Settings button will
     * lauch Settings Options
     * */
    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    
        alertDialog.setTitle("GPS Settings");
        alertDialog
                .setMessage("GPS is not enabled. Do you want to go to settings menu?");
    
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        context.startActivity(intent);
                    }
                });
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }
    
    @Override
    public void onLocationChanged(Location location) {
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }}
    

以这种方式使用上面的代码,只要你想在任何活动中

MyLocationListener mylistner = new MyLocationListener(context);
        double lat = mylistner.latitude;
        double lon = mylistner.longitude;

注意:我的清单中也有打击权限

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>   
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
于 2013-01-16T10:55:15.890 回答