2013-01-17 3 views
0

Android Maps API v2를 기반으로 위치 앱을 만들려고합니다. 내 위치 지점의 마커와 위치 정확도를위한 GroundOverlay 서클을 추가합니다. 그러나 GroundOverlay setPosition 함수는 약간 다릅니다. 이유를 찾을 수 없습니다. 내가 라인Android GroundOverlay가 잘못된 위치를 설정합니다.

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 13)); 

시계에 디버거에서 응용 프로그램을 일시 중지하면

 package com.example.locator; 

    import com.google.android.gms.maps.CameraUpdateFactory; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.SupportMapFragment; 
    import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
    import com.google.android.gms.maps.model.GroundOverlay; 
    import com.google.android.gms.maps.model.GroundOverlayOptions; 
    import com.google.android.gms.maps.model.LatLng; 
    import com.google.android.gms.maps.model.Marker; 
    import com.google.android.gms.maps.model.MarkerOptions; 

    import android.location.Criteria; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.content.Context; 
    import android.support.v4.app.FragmentActivity; 
    import android.view.Menu; 

    public class MainActivity extends FragmentActivity implements LocationListener 
    { 
     GoogleMap mMap; 
     Location    lastLocation = null; 
     private LocationManager locationManager; 
     private String provider; 
     Marker curLoc = null; 
     GroundOverlay curLocCircle = null; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.375675950685434, 29.83346939086914), 13)); 

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      Criteria criteria = new Criteria(); 
      provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 

      if (location != null) { 
       onLocationChanged(location); 
      } else { 
       //empty 
      } 
      locationManager.requestLocationUpdates(provider, 400, 1, this); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.activity_main, menu); 
      return true; 
     } 


     public void onLocationChanged(Location location) { 
      if (location != null) { 

       if (null != curLoc) { 
        curLoc.remove(); 
       } 
       if (null != curLocCircle) { 
        curLocCircle.remove(); 
       } 

       LatLng loc = new LatLng(location.getLatitude(),location.getLongitude()); 
       curLoc = mMap.addMarker(new MarkerOptions() 
       .position(loc) 
       .title("My location") 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.location32))); 

       float accuracy = location.getAccuracy(); 

       curLocCircle = mMap.addGroundOverlay(new GroundOverlayOptions() 
         .image(BitmapDescriptorFactory 
           .fromResource(R.drawable.location_circle)) 
         .position(loc, accuracy) 
         .transparency(0) 
       ); 
       curLocCircle.setPosition(loc); 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 13)); 
      } 
     } 

     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

    } 

는이 데이터를 보여줍니다

curLocCircle.getPosition() lat/lng: (22.397815012772206,29.954818561673164) 
location.getLatitude()  22.397815 
location.getLongitude()  29.9548184 
loc       lat/lng: (22.397815,29.9548184) 

curLocCircle.getPosition()을 어떻게 든로 setPosition (LOC) 후 잘못된 위치 기록을 보여줍니다; 도와주세요.

답변

1

문제는 마커 앵커에서 발생했습니다. 로 변경 :

curLoc = mMap.addMarker(new MarkerOptions() 
.position(loc) 
.anchor(0.5f, 0.5f) 
.title("My location") 
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location32))); 

그리고 모든 것이 괜찮습니다. GroundOverlay의 위치가 정의 된 위치와 다른 이유는 아직 모르지만 걱정하지 않습니다. 좋아 보인다.

관련 문제