2012-01-09 137 views
-1

我使用三星spica 1.5测试当前gps位置应用程序,但它不显示我当前的位置,它只是向我显示另一个位置。Android当前gps位置不正确

这里是我的代码:

public class MaptestActivity extends MapActivity implements LocationListener { 
    private MapView mapView = null; 
     private LocationManager lm = null; 
     private double lat = 0; 
     private double lng = 0; 
     private MapController mc = null; 
     private MyLocationOverlay myLocation = null; 
     private String current; 
     private PositionMarkersList positionMarkersList = null; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mapView = (MapView) this.findViewById(R.id.mapView); 
     mapView.setBuiltInZoomControls(true); 

     lm = (LocationManager) this.getSystemService(LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this); 

     mc = mapView.getController(); 
     mc.setZoom(15); 





     myLocation = new MyLocationOverlay(getApplicationContext(), mapView); 
     myLocation.runOnFirstFix(new Runnable() {  
      public void run() { 
      mc.animateTo(myLocation.getMyLocation()); 
      mc.setZoom(17); 


      } 

     }); 

     mapView.getOverlays().add(myLocation); 
     GeoPoint pp=myLocation.getMyLocation(); 

     //addPosition(pp); 
     if (myLocation.isMyLocationEnabled()!=false) 
     { 
      GeoPoint p =myLocation.getMyLocation(); 
      lat= p.getLatitudeE6(); 
      lng= p.getLongitudeE6(); 
      Toast.makeText(getBaseContext(), 
        "geolocalisation activÈ lat: "+lat+ " long: " +lng, 
        Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 

      Toast.makeText(getBaseContext(), 
        "geolocalisation desactivÈe" , 
        Toast.LENGTH_SHORT).show(); 
     } 



     } 

     @Override 
     protected void onResume() { 
     super.onResume(); 
     myLocation.enableMyLocation(); 
     myLocation.enableCompass(); 
     } 

     @Override 
     protected boolean isRouteDisplayed() { 
     return false; 
     } 

     @Override 
     public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_S) { 
      mapView.setSatellite(!mapView.isSatellite()); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
     } 

     public void onLocationChanged(Location location) { 
     lat = location.getLatitude(); 
     lng = location.getLongitude(); 
     Toast.makeText(
      getBaseContext(), 
      "Location change to : Latitude = " + lat + " Longitude = " 
       + lng, Toast.LENGTH_SHORT).show(); 
     GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
     mc.animateTo(p); 
     mc.setCenter(p); 
     } 

     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 


//ajout d'une position 

     private void addPosition(GeoPoint geoPoint) { 
      OverlayItem overlayitem = new OverlayItem(geoPoint, current, current); 
      positionMarkersList.addMarker(overlayitem); 
     } 

} 

有谁对此,请任何想法?

当我点击运行来测试应用程序,我有测试手段列表(虚拟设备...),但有一个“!”靠近电话的名字,我不知道为什么。

谢谢!

回答

1

您必须使用Google API制作虚拟设备。

此外,在显示设备位置的情况下,我可以知道地图上显示的位置距离当前位置有多远吗?

+0

我在突尼斯,它让我在法国的位置!我正在测试真正的设备! sumsung galaxy spica为什么不起作用? – 2012-01-09 13:39:37

+0

当我做了测试,我的消息是“geolocalisation desactivated”!这可能与我的位置或类似的东西有关?可能在突尼斯我不喜欢gps在这里工作! – 2012-01-09 13:50:11

+0

您的手机没有获得当前位置,因此您的设备将返回最后一个已知位置。据我所知。您可以在设备上运行Google Map应用程序,清除应用程序的所有缓存数据,然后通过这样做重新加载该应用程序以获取当前位置,您的设备将尝试获取当前位置。 – rajpara 2012-01-09 13:53:28

1

请务必在您的AndroidManifest.xml文件中包含位置服务的正确权限!

GPS: ACCESS_FINE_LOCATION

WIFI: ACCESS_COARSE_LOCATION

所有权限:can be found here

+0

是的,我包括所有这些权限 <使用权限android:name =“android.permission.ACCESS_COARSE_LOCATION”> <使用权限android:name =“android.permission.ACCESS_FINE_LOCATION”> \t \t \t \t <使用权限android:name =”android.permission.LOCATION“/> – 2012-01-09 14:10:43

1
public Location getCurrentLocation() { 
    try { 

     LocationManager lm = (LocationManager)  getSystemService(Context.LOCATION_SERVICE); 
     // ---Get the status of GPS--- 
     boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // If GPS is not enable then it will be on 
     if (!isGPS) { 
      Intent intent = new Intent(
        "android.location.GPS_ENABLED_CHANGE"); 
      intent.putExtra("enabled", true); 
      sendBroadcast(intent); 
     } 

     Location location = lm 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location == null) { 
      location = lm 
        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     } 
     return location; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
}