2013-01-17 122 views
0

我尝试制作基于Android Maps API v2的位置应用。我为我的定位点添加标记,并为定位精度添加GroundOverlay圆。但GroundOverlay的setPosition函数略有不同。我找不到原因。Android GroundOverlay设置不正确的位置

 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 

     } 

    } 

当我在调试暂停在线

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

手表应用示出了这样的数据:

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的位置与定义的位置不同,但我不在乎。看上去不错。