2011-12-12 184 views
0

我在android開發方面經驗不足,我想在地圖上顯示當前位置。但它顯示了我在加拿大不同的位置。這裏是我的代碼:android gps如何顯示當前位置

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

這是所有代碼是。我沒有看到任何對你的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); 
}