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

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

问题描述

欢呼声中,我试图通过机器人来得到当前的GPS位置。我跟着<一个href="http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/">this教程和 Vogellas文章以及。虽然它不能正常工作。当使用LocationManager.NETWORK_PROVIDER我总是得到51纬度和经度9 - 不管我的立场。当使用LocationManager.GPS_PROVIDER,我得到什么都没有。

使用GMaps实现时,虽然一切正常:S不知道为什么。我怎样才能得到当前GPS位置就像GMaps实现呢?

下面是我的code:

 包com.example.gpslocation;

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

公共类GPS_Location扩展活动实现LocationListener的{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_gps__location);

    LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.gps__location,菜单);
    返回true;
}

@覆盖
公共无效onLocationChanged(位置定位){
    // TODO自动生成方法存根

    INT纬度=(int)的(location.getLatitude());
    INT经度=(int)的(location.getLongitude());

    Log.i(Geo_Location,纬度:+纬度+,经度:+经度);
}

@覆盖
公共无效onProviderDisabled(字符串提供商){
    // TODO自动生成方法存根

}

@覆盖
公共无效onProviderEnabled(字符串提供商){
    // TODO自动生成方法存根

}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    // TODO自动生成方法存根

}

}
 

解决方案

下面是你的问题:

  INT纬度=(INT)(location.getLatitude());
INT经度=(int)的(location.getLongitude());
 

经纬度 - 值,因为他们重新present度的位置。

由他们铸造 INT ,你放弃了逗号,这使得一个很大的区别背后的一切。请参见十进制度 - 维基

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.

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

Here's my code:

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

}

}

解决方案

Here's your problem:

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

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

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天全站免登陆