0

我想在用户打开活动后立即显示用户的当前位置。

这是我到目前为止编写的代码。

package com.android.carmanager;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class CarFinder extends MapActivity{
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.carfinder);
    mapView = (MapView) findViewById(R.id.mapView);
      mapView.setStreetView(true);
      mapView.setBuiltInZoomControls(true);
      mapView.displayZoomControls(true);

      mapController = mapView.getController();
      mapController.setZoom(16);

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

      locationListener = new GPSLocationListener();

      locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                0, 
                0, 
                locationListener);
  }
private  class GPSLocationListener implements LocationListener 
{
  public void onLocationChanged(Location location) {
    if (location != null) {
      GeoPoint point = new GeoPoint(
          (int) (location.getLatitude() * 1E6), 
          (int) (location.getLongitude() * 1E6));

      Toast.makeText(getBaseContext(), 
          "Latitude: " + location.getLatitude() + 
          " Longitude: " + location.getLongitude(), 
          Toast.LENGTH_SHORT).show();

      mapController.animateTo(point);
      mapController.setZoom(16);
      mapView.invalidate();
    }
  }

public void onProviderDisabled(String arg0) {
    // 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

}   
}

@Override
  protected boolean isRouteDisplayed(){
      //TODO Auto-generated method stub
      return false;

  }
  }

还要在此处附加清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.carmanager"
android:versionCode="1"
android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="15" />
<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.INTERNET" ></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
        <uses-library android:name = "com.google.android.maps" />

    <activity android:name=".CarManagerActivity" android:label= "@string/app_name">
    <intent-filter> 
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
    <activity android:name=".MileageCalculator" android:label= "@string/app_name"></activity>
    <activity android:name=".About" android:label= "@string/app_name"></activity>
 <activity android:name=".CarFinder" android:label="@string/app_name"></activity>
 </application>

</manifest>

如果需要,这是错误日志 cat 文件。就定位位置而言,我找不到任何错误。

05-27 16:07:05.647: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:05.697: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:06.156: D/gralloc_goldfish(706): Emulator without GPU emulation detected.
05-27 16:07:06.167: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:06.197: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:25.666: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:25.808: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:25.868: D/dalvikvm(706): GC_CONCURRENT freed 161K, 3% free 9352K/9607K, paused 12ms+7ms
05-27 16:07:25.868: W/CursorWrapperInner(706): Cursor finalized without prior close()
05-27 16:07:25.868: W/CursorWrapperInner(706): Cursor finalized without prior close()
05-27 16:07:26.186: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:26.326: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:26.396: D/dalvikvm(706): GC_CONCURRENT freed 40K, 3% free 9764K/9991K, paused 12ms+21ms
05-27 16:07:26.436: I/Maps.MyLocationOverlay(706): Request updates from gps
05-27 16:07:26.656: E/ZoomButtonsController(706): Cannot make the zoom controller visible if the owner view is not attached to a window.
05-27 16:07:26.746: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:26.767: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:26.827: I/MapActivity(706): Handling network change notification:CONNECTED
05-27 16:07:26.827: E/MapActivity(706): Couldn't get connection factory client
05-27 16:07:27.137: D/dalvikvm(706): GC_CONCURRENT freed 40K, 3% free 10167K/10375K, paused 8ms+6ms
05-27 16:07:27.188: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:27.227: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:27.887: D/dalvikvm(706): GC_CONCURRENT freed 375K, 5% free 10237K/10759K, paused 12ms+6ms

但是当我打开活动时,地图会打开,并且位置无处可见。

它甚至不显示经度/纬度。

如何调整我的代码以显示当前位置?

4

3 回答 3

1

第 1 点:根据上述评论中的@Shabbir,建议使用MyLocationOverlay.


第 2 点:

但是当我打开活动时,地图会打开,并且位置无处可见。

MyLocationOverlay需要时间来修复位置,所以它从不显示任何内容开始。它仅显示找到第一个修复后的位置。

如果您在启动此活动之前已经知道您的位置,则可以将 MyLocationOverlay 子类化以接受defaultLocation构造函数中的参数。然后重写这样的draw()方法:

public synchronized boolean draw(Canvas canvas, MapView mapView,
    boolean shadow, long when)
{
    //if no location found
    if(getLastFix()==null && defaultLocation!=null)
    {
        drawMyLocation(canvas, mapView, defaultLocation, DataUtilities.locationToGeoPoint(defaultLocation), when);
    }

    return super.draw(canvas, mapView, shadow, when);
}

如果尚未找到修复程序,请绘制您的默认位置,否则只需让基类做它的事情。

使用此方法,您可以立即显示初始位置 ( defaultLocation),同时等待您的设备获得更好的修复。

于 2012-10-17T12:53:24.020 回答
0

使用这个片段

   List<Overlay> over_lay = mapView.getOverlays();
   MyLocationOverlay mylocationoverlay = new MyLocationOverlay(this, mapView);
   over_lay.add(mylocationoverlay);
于 2012-12-22T05:05:12.070 回答
0

看看这个... http://android-er.blogspot.in/2009/11/mapview-to-center-on-current-location.html

于 2012-06-03T14:45:37.237 回答