Current Loan does not select in android

Now I go to the project related to the Google Map service. I would like to show my current location on my map. For this I have an idea of ​​using itemizedoverlay. I am fetching the current location using Geopoint. But when I use itemizedoverlay in my code, the location cannot be found and I use that in my OnCreate method. If I go to the OnLocationChanged method, then the GeoPoint (point) will not be raised by itemizedoverlay and will fail and the application will be squashed in its current state.

        MapView mapView;
    Location location;
    LocationManager locationmanager;
    LocationListener locationlistener; 

        public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

      mapView = (MapView) findViewById(R.id.mapView);
      mapView.setTraffic(true);
      mapView.setBuiltInZoomControls(true);

        //locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
   // locationlistener = new GPSLocationListener();
      //  locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER 0,0,locationlistener);                                                                                         

      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();

      List<Overlay> mapOverlays = mapView.getOverlays();
      Drawable drawable = this.getResources().getDrawable(R.drawable.icon);


          HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
          OverlayItem overlayitem = new OverlayItem(point,"","" );
          itemizedoverlay.addOverlay(overlayitem);
          mapOverlays.add(itemizedoverlay);

}

/*      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));

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

            OverlayItem overlayitem = new OverlayItem(point,"","" );
              itemizedoverlay.addOverlay(overlayitem);
              mapOverlays.add(itemizedoverlay);

}
        } */


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

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

    }

    @Override
    public void onProviderDisabled(String provider) {

Toast.makeText(getBaseContext(),"GPS Disabled",Toast.LENGTH_SHORT).show();      

    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(getBaseContext(),"GPS Enabled",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

      

Can you suggest me a solution for this? Thanks in advance and sorry if I made any mistakes here.

+3


source to share


4 answers


You can use com.google.android.maps.MyLocationOverlay which will automatically display your location on your map. It will even update your location as you travel.

Note. You expose the callMyLocation and disableMyLocation function on onResume and onPause respectively for better performance.

@Override
    protected void onResume() {
        super.onResume();
        if (locationOverlay != null)
            locationOverlay.enableMyLocation();
    }

    @Override
    protected void onPause() {
        if (locationOverlay != null)
            locationOverlay.disableMyLocation();
        super.onPause();
    }

      



EDITED

MyLocationOverlay locationOverlay;

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      mapView = (MapView) findViewById(R.id.mapView);
      mapView.setTraffic(true);
      mapView.setBuiltInZoomControls(true);
locationOverlay = new MyLocationOverlay(this, mapView);
List<Overlay> mapOverlays = mapView.getOverlays();
 mapOverlays.add(locationOverlay);
}

      

also add onResume () and onPause () methods.

+2


source


Try with this -



/**
* Setting Google Map to provided location 
*/
 private void setMaptoLocation(Location location) {
  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(true);
  int LAT = (int)(location.getLatitude() * 1E6) ;
  int LNG = (int)(location.getLongitude() * 1E6) ;
  GeoPoint point = new GeoPoint(LAT, LNG);
  mapController = mapView.getController();
  List<Overlay> mapOverlays = mapView.getOverlays();
  Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
  HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
  OverlayItem overlayitem = new OverlayItem(point,"","" );
  itemizedoverlay.addOverlay(overlayitem);
  mapOverlays.add(itemizedoverlay);
  mapController.setZoom(ZOOM_LEVEL - ZOOM_LEVEL / 2); // Set zoom level per requierment
  mapController.animateTo(point);

 }

      

+2


source


I think you need to go with Google Maps Android API v2

For more information check Google Maps Android API v2 .

please check the link for the example code from the above link. sample code

0


source


location is not where you initialized it in onCreate () , so I think its null. And one more thing, if you got Lat and Long, the detailed ones will no doubt show the location on the map. Check permission Access_Course_location and Access_fine_location in manifest.

0


source







All Articles