如何获取 Android GPS 位置 [英] How to get Android GPS location

查看:38
本文介绍了如何获取 Android GPS 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Cheers, I'm trying to get the current GPS-Location via Android. I followed this Tutorial and Vogellas article aswell. Though it ain't working. When using LocationManager.NETWORK_PROVIDER I'm always getting a latitude of 51 and longitude of 9 - no matter where I stand. When using LocationManager.GPS_PROVIDER, I'm getting nothing at all.

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

Though Everything works when using GMaps :S No idea why. How can I get the current GPS Location like GMaps does?

这是我的代码:

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

}

}

推荐答案

这是你的问题:

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

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

Latitude and Longitude are double-values, because they represent the location in degrees.

通过将它们转换为 int,您将丢弃逗号后面的所有内容,这会产生很大的不同.请参阅十进制度 - Wiki"

By casting them to int, you're discarding everything behind the comma, which makes a big difference. See "Decimal Degrees - Wiki"

这篇关于如何获取 Android GPS 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆