Java android gps如何显示当前位置
我在android开发方面没有丰富的经验,我想在地图上显示我当前的位置。但它告诉我一个不同的地点,在加拿大。这是我的密码:Java android gps如何显示当前位置,java,android,eclipse,Java,Android,Eclipse,我在android开发方面没有丰富的经验,我想在地图上显示我当前的位置。但它告诉我一个不同的地点,在加拿大。这是我的密码: package com.manita.mapuse; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import andr
package com.manita.mapuse;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
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 MapuseActivity extends MapActivity implements LocationListener {
private MapView mapView = null;
private MapController mc;
private GeoPoint location;
double lat;
double lon;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---use the LocationManager class to obtain GPS locations---
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
lat= loc.getLatitude();
lon= loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
} /*End of Class MyLocationListener */
/* class map view */
public MapView viewmap (View mapview, double latitude, double longitude){
this.mapView = new MapView(this,this.getResources().getString(R.string.mapKey));
this.mapView.setClickable(true);
this.mc = this.mapView.getController();
this.location = new GeoPoint((int) (latitude * 1000000.0),(int) (longitude * 1000000.0));
this.mc.setCenter(this.location);
this.mc.setZoom(17);
this.mapView.setSatellite(true);
this.mapView.invalidate();
this.setContentView(this.mapView);
mapView.setBuiltInZoomControls(true);
return mapView;
}
/* End class map view */
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@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
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
我太困惑了,你需要在这方面做很多改变,试着从我的帖子中查看一些教程,你从那里得到了很多链接 这里
这是一个很好的例子。只要复制粘贴就可以了。这就是全部代码吗?我没有看到任何对mapview对象的调用来更新纬度或经度。也许可以尝试初始化mc控制器并将这段代码添加到onLocationChanged方法中,看看会发生什么。这只是为了帮助您发现问题,实际上不要像这样保留代码,因为如果您甚至不知道MapController是否已初始化,则使用MapController是危险的
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
lat= loc.getLatitude();
lon= loc.getLongitude();
this.location = new GeoPoint((int) (latitude * 1000000.0),(int) (longitude * 1000000.0));
this.mc.setCenter(this.location);
}