1

我正在尝试实现一个提供用户位置的应用程序。我尝试了很多例子并且没有运气。最后我发现这个代码发布在stackoverflow(谷歌地图上的当前位置)。但是我遇到了错误,我完全不知道。请有人可以帮助我。

这是代码..

public class MainActivity extends Activity {

public static final String TAG = "GoogleMapsActivity";
private MapView mapView;
private LocationManager locationManager;
Geocoder geocoder;
Location location;
LocationListener locationListener;
CountDownTimer locationtimer;
MapController mapController;
MapOverlay mapOverlay = new MapOverlay();

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
    //setContentView(R.layout.googlemap);
   // initComponents();
    mapView=(MapView)findViewById(R.id.myGMap);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(true);
    mapController = mapView.getController();
    mapController.setZoom(16);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager == null) {
        Toast.makeText(MainActivity.this,
                "Location Manager Not Available", Toast.LENGTH_SHORT)
                .show();
        return;
    }
    location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null)
        location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        Toast.makeText(MainActivity.this,
                "Location Are" + lat + ":" + lng, Toast.LENGTH_SHORT)
                .show();
        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        mapController.animateTo(point, new Message());
        mapOverlay.setPointToDraw(point);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
    }
    locationListener = new LocationListener() {
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        }

        public void onProviderEnabled(String arg0) {
        }

        public void onProviderDisabled(String arg0) {
        }

        public void onLocationChanged(Location l) {
            location = l;
            locationManager.removeUpdates(this);
            if (l.getLatitude() == 0 || l.getLongitude() == 0) {
            } else {
                double lat = l.getLatitude();
                double lng = l.getLongitude();
                Toast.makeText(MainActivity.this,
                        "Location Are" + lat + ":" + lng,
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
    locationtimer = new CountDownTimer(30000, 5000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (location != null)
                locationtimer.cancel();
        }

        @Override
        public void onFinish() {
            if (location == null) {
            }
        }
    };
    locationtimer.start();
}

public MapView getMapView() {
    return this.mapView;
}



/* private void initComponents() {

    }*/

protected boolean isRouteDisplayed() {
    return false;
}

class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        super.draw(canvas, mapView, shadow);

        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_action_search);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
        return true;
    }
}




}

这是我的 logcat 消息'''

10-12 08:08:15.931: E/AndroidRuntime(306): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.example.positioning4/com.example.positioning4.MainActivity}:  android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.maps.MapView

10-12 08:08:15.931: E/AndroidRuntime(306): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.maps.MapView

这是我的 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.android.maps.MapView
    android:id="@+id/myGMap"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="0sLxxvkp8prR4adOMGAG9vsrukJSDiB93vRGN1g"
    />

4

2 回答 2

0

尝试从 MapActivity 扩展您的 MainActivity。

    public class MainActivity extends MapActivity {
}
于 2012-10-12T03:06:44.777 回答
0

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

到你AndroidManifest.xmlapplication标签下

更新

只是扩展MapActivity而不是Activity

于 2012-10-12T02:57:47.867 回答