2012-09-15 162 views
1

我做了一个Android应用程序,它在Android Screen的上半部分显示了Google Map,在Android屏幕的下半部分显示了一个TextView。在模拟器上工作,但不在真正的Android设备上

而且我Google Maps(on the Top half of the android screen),我对我目前的位置绘制Circle,而我从DDMS perspective in the emulator通过我current location (lat and long values)。它在模拟器中工作正常。

当我在模拟器上启动应用程序时,首先它会在android屏幕的上半部分显示一个Google Map,在下半部分显示一个TextView。然后使用DDMS Perspective,我传递当前位置(lat和long值),并在传递之后,在Google地图上的当前位置绘制一个Circle。

在模拟器中一切正常。

问题陈述: -

但是,当我试图对现实Android Phone同样的事情,只有我Google Maps是越来越正常显示在Android屏幕的上半部分和下半部TextView的Android屏幕。但它没有绘制Circle on my Current Location

我连接到我的Android手机上的wi-fi。而我不认为我需要在Android手机上传递经度和纬度值,对吧?它应该自动获取当前位置?

或者我需要在Android手机上做什么?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

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

    locationListener = new GPSLocationListener(); 

    locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      0, 
      0, 
      locationListener); 

    mapView = (MapView) findViewById(R.id.mapView); 
    listView = (ListView) findViewById(R.id.mylist); 
    mapView.setStreetView(true); 
    mapView.setBuiltInZoomControls(true); 

    mapController = mapView.getController(); 
    mapController.setZoom(15); 
} 

private class GPSLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      GeoPoint point = new GeoPoint(
        (int) (location.getLatitude() * 1E6), 
        (int) (location.getLongitude() * 1E6)); 

      findUsersInCurrentRadius(4,location.getLatitude(),location.getLongitude()); 

      mapController.animateTo(point); 
      mapController.setZoom(15); 

      // add marker 
      MapOverlay mapOverlay = new MapOverlay(this,android.R.drawable.star_on,R.drawable.tenm,R.drawable.twentym,R.drawable.thirtym,R.drawable.fourtym); 
      mapOverlay.setPointToDraw(point); 
      List<Overlay> listOfOverlays = mapView.getOverlays(); 
      listOfOverlays.clear(); 
      listOfOverlays.add(mapOverlay); 

      String address = ConvertPointToLocation(point); 
      Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show(); 

      mapView.invalidate(); 
     } 
    } 


class MapOverlay extends Overlay { 
    private GeoPoint pointToDraw; 
    int[] imageNames=new int[6]; 

    public MapOverlay(GPSLocationListener gpsLocationListener, 
      int currentUser, int tenm, int twentym, int thirtym, int fourtym) { 
     imageNames[0]=currentUser; 
     imageNames[1]=tenm; 
     imageNames[2]=twentym; 
     imageNames[3]=thirtym; 
     imageNames[4]=fourtym; 
    } 

    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); 
     //---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(pointToDraw, screenPts); 
     //--------------draw circle---------------------- 
     Point pt = mapView.getProjection().toPixels(pointToDraw,screenPts); 
     Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     circlePaint.setColor(0x30000000); 
     circlePaint.setStyle(Style.FILL_AND_STROKE); 

     int totalCircle=4; 
     int radius=40; 
     int centerimagesize=35; 

     for (int i = 1; i <= totalCircle; i ++) { 
      canvas.drawCircle(screenPts.x,screenPts.y, i*radius, circlePaint); 
     } 
     canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[0]), (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null); 
     super.draw(canvas,mapView,shadow); 


     return true; 
    } 
} 

而且我的AndroidManifest.xml文件 -

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.circlemapandroid" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="3" 
     android:targetSdkVersion="8" /> 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 

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

     <activity 
      android:name=".ThesisProjectAndroid" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

回答

2

我可以看到您的代码,该圆圈仅在收到第一个修复程序后才绘制。 您是否在手机设置(位置和安全)中启用了GPS?如果没有,启用它。

只有在您从GPS获得第一个修复程序后,您才能获得该圈子。你需要把GPS带到室外才能得到它。

您的位置监听器都举杯,因此,如果你得到一个吐司与地址的价值,以上都不是将解决你的问题。

最后的效率,我会移动代码来创建并添加叠加出来的位置监听器的,因为它在某种程度上重把它创建和删除每次收到新的修补程序。

1

你缺少了GPS的一个更许可,加在你的AndroidManifest.xml这个

<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission> 
+0

我已尝试通过增加在我AndroidManifest,但仍没有绘制圆我的当前位置我的Android手机上。还有什么我失踪? – ferhan

相关问题