Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android GPS应用程序不工作_Java_Android_Google Maps_Gps - Fatal编程技术网

Java Android GPS应用程序不工作

Java Android GPS应用程序不工作,java,android,google-maps,gps,Java,Android,Google Maps,Gps,我试着制作自己的应用程序并使用谷歌地图。我希望它将地图的中心设置在我当前的gps位置上,但是当我的手机上有gps锁时,我只会转到这些坐标(0,0),我不知道我哪里出错了。谢谢大家:D import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import a

我试着制作自己的应用程序并使用谷歌地图。我希望它将地图的中心设置在我当前的gps位置上,但是当我的手机上有gps锁时,我只会转到这些坐标(0,0),我不知道我哪里出错了。谢谢大家:D

import android.content.Context;
import android.location.Location;
import android.location.LocationListener; 
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class Courses extends MapActivity {

MapView map;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.courses);       
    MapView map = (MapView) findViewById (R.id.MapView);
    map.setBuiltInZoomControls(true);
    map.setSatellite(true);
    final MapController control = map.getController();


    LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));              
            control.setZoom(19);
        }

        @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

        }

    };


manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}
`

我在这里看到的第一个问题是,location.getLatitude()和location.getLatitude()返回浮点值,必须将其与1E6相乘,然后将其转换为整数,以便GeoPoint可以接受。这也解释了为什么坐标约为0,0

我建议替换你的
control.setCenter(新的地理点((int)location.getLatitude(),(int)location.getLongitude())带有
control.setCenter(新的地理点((int)(location.getLatitude()*1E6),(int)(location.getLatitude()*1E6))


试试看,应该可以了。

你们在布局中设置了地图api键了吗?api键不应该保密吗?谢谢!!!:我还有一个问题,你知道如何从你的gps上获取坐标,这样你以后就可以在谷歌地图上查看你所在的位置,我想通过按下一个按钮来确保坐标的安全。再次感谢!这实际上是我本周开发的应用程序的一部分。然而,我做了一个服务。它只是每秒将当前GPS坐标和当前UTC时间存储到数据库中。我还以最小的准确性增强了它,并在跟踪过程中显示通知。以下是消息来源:-我希望这有帮助。