简体   繁体   中英

Working on Emulator but not on the real Android device

I have made an Android Application which shows a Google Map on the Top Half of the Android Screen and in the bottom half of the Android screen it shows a TextView.

And on my Google Maps(on the Top half of the android screen) , I am drawing a Circle on my current location, and I am passing my current location (lat and long values) from the DDMS perspective in the emulator . And it is working fine in the emulator.

When I launch an application on the emulator, first of all it shows a Google Map on the Top Half of the android screen and in the bottom half, it shows a TextView. And then with the use of DDMS Perspective, I am passing my current location (lat and long values) and after passing, it draws a Circle on my current location on the Google maps.

And everything works fine in the emulator.

Problem Statement:-

But when I am trying the same thing on the real Android Phone , only my Google Maps is getting shown properly on the Top half of the android screen and the TextView on the Bottom Half of the Android Screen. But it is not drawing the Circle on my Current Location .

I am connected to wi-fi on my Android phone. And I don't think so I need to pass the Latitude and Longitude values on the Android Phone, right? It should take the current location automatically right?

Or Is there anything I need to do on my Android Phone?

@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;
    }
} 

And my AndroidManifest.xml file-

<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>

I can see from your code, that the circle is only drawn after the first fix is received. Do you have GPS enabled in the phone settings (Location and security)? If not, enable it.

You will get the circle only after you get the first fix from GPS. You need to take the GPS outdoor to get it.

You have a Toast in Location Listener, so if you are getting a Toast with the value of the address, none of the above will solve your problem.

Finally for efficiency , I would move the code to create and add the overlay out of the Location Listener, as it's somehow heavy to have it created and removed everytime you receive a new fix.

您缺少GPS的另一个权限,请在AndroidManifest.xml中添加此权限

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM