2011-02-17 2 views
0

주제로 저는 현재 스웨덴에 있지만 내지도를 시작할 때 나는 영국에 있음을 보여줍니다.android : mapView가 올바른 사용자 위치를 제공하지 않음

내 코드는 다음과 같습니다

private MapListener myMapViewListener; 
private MapController mapController; 
private GeoPoint test; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mapview); 
    setupListener(); 

    MapView mapView = (MapView) findViewById(R.id.mapview); 
    mapController = mapView.getController(); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setClickable(true); 

    mapController.setZoom(7); // defualt zoom  
} 

private void setupListener() { 

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    MapListener listener = new MapListener(); 
    MapView mapView = (MapView) findViewById(R.id.mapview); 

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000); 
      mapController.animateTo(test); 
     } 

     @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 

     } 
    }; 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
} 

}

내가 추가 한 ........

<uses-library android:name="com.google.android.maps" /> 
</application> 

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses- permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

    </manifest> 

을 ..... 내 매니페스트에.

저는 http://developer.android.com/guide/topics/location/obtaining-user-location.html을 가이드로 사용하고 있습니다.

도움에 대해 매우 감사드립니다.

답변

3

곱셈을 수행하기 전에 int로 변환되므로 계산이 필요하지 않습니다. 다음으로 교체하십시오.

test = new GeoPoint((int)(location.getLatitude()*1000000), 
    (int)(location.getLongitude()*1000000)); 

당신은 또한 당신이지도에서 위치를 그리려는 경우 MyLocationOverlay을 확인 할 수 있습니다.

MyLocationOverlay를 사용하려면.

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView); 
mapView.getOverlays().add(myLocationOverlay); 
myLocationOverlay.enableMyLocation(); 

일시 정지에 당신은 또한 호출해야 myLocationOverlay.disableMyLocation() 그래서 당신의 활동이 배경에있는 경우는 배터리를 소모하지 않습니다.

+0

내가 시도조차 작동하지 않았다 : \t \t \t 이중 위도 = 위치. getLatitude() * 1000000; \t \t \t double alt = location.getAltitude() * 1000000; \t \t \t 시험 = 새로운 GeoPoint ((int) lat, (int) alt); \t \t mapController.animateTo (test); –

+0

@ Gwing-Ben Yuen 당신은 경도 대신에 고도를 사용하고 있습니다. –

+0

하하, 고맙다고하지 않았다. –

4

실제로 지리적 포인트를 잘못 작성하는 중입니다. 여기에서 위도와 고도를 설정하고 있습니다.

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000) 

당신은

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getLongitude()*1000000) 

또한 MyLocationOverlay 클래스를 체크 아웃하려고 위도와 경도를 설정할 수 있습니다

+1

광산을 정확하게 표시 할 수 있습니까? 내 스택 오버 플로우 점수를 강화하고있어 :) –

관련 문제