我在Android开发方面经验不足,我想在地图上显示我当前的位置。但它显示了我在加拿大的不同位置。这是我的代码:
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;
}
}
答案 0 :(得分:0)
我太混淆了你需要做很多改变的尝试从我的帖子中查看一些教程你从那里得到很多链接
答案 1 :(得分:0)
此处http://www.vogella.de/articles/AndroidLocationAPI/article.html#overview_maps 是一个非常好的例子。只需复制粘贴即可。
答案 2 :(得分:0)
这是所有的代码吗?我没有看到对mapview对象的任何调用来更新纬度或经度。也许尝试启动mc控制器并将这段代码添加到onLocationChanged方法中,看看会发生什么。这只是为了帮助您找到问题,实际上不要留下这样的代码,因为如果您甚至不知道它是否已被引入,那么使用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);
}