Google Map API代码未显示当前位置

时间:2012-05-27 10:56:54

标签: android google-maps android-maps

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

这是我到目前为止编码的内容。

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

但是当我打开活动时,地图会打开,无法看到位置。

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

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

3 个答案:

答案 0 :(得分: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),同时等待设备获得更好的修复。

答案 1 :(得分:0)

答案 2 :(得分:0)

使用此代码段

   List<Overlay> over_lay = mapView.getOverlays();
   MyLocationOverlay mylocationoverlay = new MyLocationOverlay(this, mapView);
   over_lay.add(mylocationoverlay);