无法在Android中确定位置

时间:2014-03-06 12:02:22

标签: android android-mapview android-maps android-maps-v2 mapactivity

我正在尝试创建Android,在地图视图中显示用户的位置。但是在运行此应用程序时它会显示“无法确定位置”。我陷入困境。您可以帮我处理我的代码或链接到如何正确实现这一点的指南?

这是我的代码:

public class MyMapLocationActivity extends MapActivity {

private MapView mapView;
private MyLocationOverlay myLocationOverlay;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // main.xml contains a MapView
    setContentView(R.layout.main); 

    // extract MapView from layout
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    // create an overlay that shows our current location
    myLocationOverlay = new FixedMyLocationOverlay(this, mapView);

    // add this overlay to the MapView and refresh it
    mapView.getOverlays().add(myLocationOverlay);
    mapView.postInvalidate();

    // call convenience method that zooms map on our location
    zoomToMyLocation();
}

@Override
protected void onResume() {
    super.onResume();
    // when our activity resumes, we want to register for location updates
    myLocationOverlay.enableMyLocation();
}

@Override
protected void onPause() {
    super.onPause();
    // when our activity pauses, we want to remove listening for location updates
    myLocationOverlay.disableMyLocation();
}

/**
 * This method zooms to the user's location with a zoom level of 10.
 */
private void zoomToMyLocation() {
    GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
    if(myLocationGeoPoint != null) {
        mapView.getController().animateTo(myLocationGeoPoint);
        mapView.getController().setZoom(10);
    }
    else {
        Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
    }
}

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

FixedMyLocationOverlay.java

public class FixedMyLocationOverlay extends MyLocationOverlay {

private boolean bugged = false;

private Drawable drawable;
private Paint accuracyPaint;
private Point center;
private Point left;
private int width;
private int height;

public FixedMyLocationOverlay(Context context, MapView mapView) {
    super(context, mapView);
}

@Override
protected void drawMyLocation(Canvas canvas, MapView mapView,
        Location lastFix, GeoPoint myLocation, long when) {
    if(!bugged) {
        try {
            super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);
        } catch (Exception e) {
            // we found a buggy phone, draw the location icons ourselves
            bugged = true;
        }
    }

    if(bugged) {
        if(drawable == null) {

            accuracyPaint = new Paint();
            accuracyPaint.setAntiAlias(true);
            accuracyPaint.setStrokeWidth(2.0f);

            drawable = mapView.getContext().getResources().getDrawable(R.drawable.ic_maps_indicator_current_position);
            width = drawable.getIntrinsicWidth();
            height = drawable.getIntrinsicHeight();
            center = new Point();
            left = new Point();
        }

        Projection projection = mapView.getProjection();
        double latitude = lastFix.getLatitude();
        double longitude = lastFix.getLongitude();
        float accuracy = lastFix.getAccuracy();

        float[] result = new float[1];

        Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
        float longitudeLineDistance = result[0];

        GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudeLineDistance)*1e6));
        projection.toPixels(leftGeo, left);
        projection.toPixels(myLocation, center);
        int radius = center.x - left.x;

        accuracyPaint.setColor(0xff6666ff);
        accuracyPaint.setStyle(Style.STROKE);
        canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

        accuracyPaint.setColor(0x186666ff);
        accuracyPaint.setStyle(Style.FILL);
        canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

        drawable.setBounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2);
        drawable.draw(canvas);
    }
}}

在清单文件中,我在下面添加:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

enter image description here

1 个答案:

答案 0 :(得分:1)

以下链接可能有用,它对我有用:

http://ramsandroid4all.blogspot.in/2013/06/google-maps-android-api-v2-showing.html

以上链接解释了两件事,

  1. 如何在Android中实施Google地图版本2.
  2. 如何实现位置监听器以确定用户位置。
  3. 整体代码显示了如何使用NETWORK_PROVIDER获取当前位置,并通过Google地图上的标记显示它。您的设备需要安装Google地图和互联网连接才能正常运行。以下代码是确定用户位置时最重要的部分:

    @Override
     public void onLocationChanged(Location location) {
    
       map.clear();
    
       MarkerOptions mp = new MarkerOptions();
    
       mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
    
       mp.title("my position");
    
       map.addMarker(mp);
    
       map.animateCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(location.getLatitude(), location.getLongitude()), 16));
    
      }