Android location layout

I use the following to make fun of my location on Android.

public class GPSwork extends Activity implements LocationListener{
LocationManager locationManager;
String mocLocationProvider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mocLocationProvider = LocationManager.GPS_PROVIDER; 
    setLocation(28.574853,78.063201);
}

public void setLocation(double latitude, double longitude) {   
    locationManager.addTestProvider(mocLocationProvider, false, true, false, false, false, false, false, 0, 5);
    locationManager.setTestProviderEnabled(mocLocationProvider, true);
    locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);
    Location loc = new Location(mocLocationProvider);
    loc.setTime(System.currentTimeMillis());
    loc.setLatitude(latitude);
    loc.setLongitude(longitude);
    locationManager.setTestProviderLocation(mocLocationProvider, loc);   
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub      
}

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

But it does not work. I want to see it in a real device and a bubble blinking in this place, but I got nothing. Please correct my code and help me. I used permissions.

I want to create a similatiom location app. Usually, when we open Google maps, it will show our current location with blue blinking in our location. Similarly, if I changed the coordinates, i.e. latitude and latitude, this should show that am in this place with a blue blinking,

! [alt text] [1] this is a short screen.

My XML tone is

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.maps.MapView 
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0Sy8vgJlQkFxcisUAkrxu3xN33dqFgBetCg4jxg"
    />

+3
source share
3 answers

, google:

public class MapsActivity extends MapActivity 
{    

    MapView mapView;
MapController mc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mapView = (MapView)findViewById(R.id.mapView);
        //zoom controlls
        mc = mapView.getController();
        mc.setZoom(12);
        setContentView(R.layout.main);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

AndroidManifest.xml :

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

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

, onCreate:

 String coordinates[] = {"1.352566007", "103.78921587"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 p = new GeoPoint(
       (int) (lat * 1E6), 
       (int) (lng * 1E6));

 mc.animateTo(p);
 mc.setZoom(17); 
 mapView.invalidate();

, , . MapsActivity :

class MapOverlay extends com.google.android.maps.Overlay
    {
        @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(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.my_bubble);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    } 

, . :

//---Add a location marker---
 MapOverlay mapOverlay = new MapOverlay();
 List<Overlay> listOfOverlays = mapView.getOverlays();
 listOfOverlays.clear();
 listOfOverlays.add(mapOverlay);        

 mapView.invalidate();

. google goi Google Maps api. Google Maps api , Google Maps Android

+2

gps android. , . :

public MyGPS implements LocationListener{

    public LocationManager lm = null;
    private MainActivity SystemService = null;
    //lat, lng
    private double mLongitude = 0;
    private double mLatitude = 0;

    public MyGPS(MainActivity sservice){
        this.SystemService = sservice;
        this.startLocationService();
    }

    public void startLocationService(){
        this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
        this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
    }

    public void onLocationChanged(Location location) {
        location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try {
            this.mLongitude = location.getLongitude();
            this.mLatitude = location.getLatitude();
        } catch (NullPointerException e) {
            Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
        }
    }
}

onCreate , placelistener gps-. lng lat, , . , :

:

public void onLocationChanged(Location location) {
        location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try {
            this.mLongitude = location.getLongitude();
            this.mLatitude = location.getLatitude();
            Message msg = Message.obtain();
            msg.what = UPDATE_LOCATION;
            this.SystemService.myViewUpdateHandler.sendMessage(msg);
        } catch (NullPointerException e) {
            Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
        }
    }

:

Handler myViewUpdateHandler = new Handler(){

        public void handleMessage(Message msg) {
                switch (msg.what) {
                case UPDATE_LOCATION:
                //access lat and lng   
         }));
               }

                super.handleMessage(msg);
        }
};
0

, MyLocationOverlay onDraw , superclass.onDraw.

0
source

Source: https://habr.com/ru/post/1766122/


All Articles