如何获得Android GPS位置

Jus*_*sti 31 java gps android location google-maps

干杯,我正试图通过Android获得当前的GPS位置.我也遵循了本教程Vogellas的文章.虽然它不起作用.当使用LocationManager.NETWORK_PROVIDER时,无论我站在哪里,我总是得到51的纬度和9的经度.使用LocationManager.GPS_PROVIDER时,我什么都没得到.

虽然使用GMaps时一切正常:S不知道为什么.如何像GMaps一样获得当前的GPS位置?

这是我的代码:

package com.example.gpslocation;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;

public class GPS_Location extends Activity implements LocationListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps__location);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.gps__location, menu);
    return true;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    int latitude = (int) (location.getLatitude());
    int longitude = (int) (location.getLongitude());

    Log.i("Geo_Location", "Latitude: " + latitude + ", Longitude: " + longitude);
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}
Run Code Online (Sandbox Code Playgroud)

Luk*_*uth 26

这是你的问题:

int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
Run Code Online (Sandbox Code Playgroud)

纬度和经度是值double,因为它们以度为单位表示位置.

通过投射它们int,你将丢弃逗号后面的所有内容,这会产生很大的不同.请参阅"十进制度 - 维基"


Ser*_*mir 21

试试看 :

public LatLng getLocation()
    {
     // Get the location manager
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
     Criteria criteria = new Criteria();
     String bestProvider = locationManager.getBestProvider(criteria, false);
     Location location = locationManager.getLastKnownLocation(bestProvider);
     Double lat,lon;
     try {
       lat = location.getLatitude ();
       lon = location.getLongitude ();
       return new LatLng(lat, lon);
     }
     catch (NullPointerException e){
         e.printStackTrace();
       return null;
     }
    }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答,虽然我已经尝试了你的代码,只是将我自己的代码剪切成最简单的解决方案来向你展示问题; P谢谢你们 (4认同)

小智 12

这段代码有一个问题:

int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
Run Code Online (Sandbox Code Playgroud)

你可以int改为double

double latitude = location.getLatitude();
double longitude = location.getLongitude();
Run Code Online (Sandbox Code Playgroud)


Ale*_*nov 9

最初的问题通过改变latlon加倍来解决.

我想在解决方案中添加评论.Location location = locationManager.getLastKnownLocation(bestProvider); 当其他应用程序正在为此找到时,它可以找出最后的已知位置.例如,如果自设备启动以来没有应用程序执行此操作,则代码将返回零(最近我花了一些时间来计算出来).

此外,当不需要时,停止收听也是一种很好的做法 locationManager.removeUpdates(this);

此外,即使具有权限manifest,代码也可在设备上的Android设置中启用位置服务时运行.