I'd like to get Geo location by using GPS provider. So my code will be like this
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
LocationManager lm;
TextView textLatitude, textLongitude;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLatitude = (TextView)findViewById(R.id.textLatitude);
textLongitude = (TextView)findViewById(R.id.textLongitude);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
/* Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);*/
}
public void onResume() {
super.onResume();
setup();
}
public void onStart() {
super.onStart();
boolean gpsEnabled, networkEnabled;
gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("LocationService","GPS Status: "+gpsEnabled);
if(!gpsEnabled) {
networkEnabled =
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!networkEnabled) {
Intent intent =
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
}
public void onStop() {
super.onStop();
lm.removeUpdates(listener);
}
public void setup() {
lm.removeUpdates(listener);
String latitude = "Unknown";
String longitude = "Unknown";
Location gpsLocation = requestUpdatesFromProvider(
LocationManager.GPS_PROVIDER, "GPS not supported");
if(gpsLocation != null) {
Log.d("Location Service","GET FROM GPS");
latitude = String.format("%.7f", gpsLocation.getLatitude());
longitude = String.format("%.7f", gpsLocation.getLongitude());
}
textLatitude.setText(latitude);
textLongitude.setText(longitude);
}
public Location requestUpdatesFromProvider(final String provider
, String error) {
Location location = null;
if (lm.isProviderEnabled(provider)) {
lm.requestLocationUpdates(provider, 0, 0, listener);
Log.d("LocationService","Requesting...");
location = lm.getLastKnownLocation(provider);
//Log.d("LocationSerivce", "Geo :"+location.getLatitude());
} else {
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
return location;
}
public LocationListener listener = new LocationListener() {
public void onLocationChanged(Location location) {
textLatitude.setText(String.format("%.7f", location.getLatitude()));
textLongitude.setText(String.format("%.7f",location.getLongitude()));
}
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider
, int status, Bundle extras) { }
};
}
The result is ... IT DOESN'T WORK. What have I done wrong?
I've also added permission to manifest file already.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name
="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.locationservice.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I search a lot from Internet and I got that it'll work if I combine with Network provider and I have to use internet to get my location. This is not what I want.
I want to get Location from GPS only because I'll use for offline application.
Could you give me any great suggestion. I'll thanks a lot
Related
I would like to create an app that can calculate distance between 2 location, the first is find with GPS,while the second is set by me. The app doesn't open, and the error is in the
line 61: distance = location.distanceTo(locationSede);
Help me!
package com.example.prova.mapapplicationprove;
import android.Manifest;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity implements LocationListener {
private float distance;
private float[] distfloat = new float[1];
private final LatLng POINT = new LatLng(0, 0); //longitudine e latitudine
LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
LatLng sede = new LatLng(45.012447, 7.621013);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(POINT, 1));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
map.addMarker(new MarkerOptions().position(sede).title("Soluzioni"));
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
if(provider != null & !provider.equals(""))
{
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(provider, 2000, 1, this);
Location locationSede = new Location("Sede");
locationSede.setLatitude(45.012447);
locationSede.setLongitude(7.621013);
distance = location.distanceTo(locationSede); //LINE NUMBER 61
// location.distanceBetween(sede.latitude,sede.longitude,location.getLatitude(),location.getLongitude(),distfloat);
if(location!=null)
{
onLocationChanged(location);
}
else{
Toast.makeText(getApplicationContext(),"Location not found, GPS disabled.",Toast.LENGTH_LONG ).show();
}
}
else
{
Toast.makeText(getApplicationContext(),"Provider is null.",Toast.LENGTH_LONG).show();
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
#Override
public void onLocationChanged(Location location) {
Toast.makeText(this,"Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude(),Toast.LENGTH_LONG ).show();
TextView textView2 = (TextView) findViewById(R.id.textview2);
TextView textView3 = (TextView) findViewById(R.id.textview3);
TextView textView4 = (TextView) findViewById(R.id.textview4);
String distanza = String.valueOf(distance);
//String distanza = String.valueOf(distfloat[0]);
textView2.setText("Latitude="+location.getLatitude());
textView3.setText("Longitude="+ location.getLongitude());
textView4.setText("Distance="+ distanza);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prova.mapapplicationprove">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Per usare il GPS -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.prova.mapapplicationprove.MainActivity" />
</activity>
</application>
</manifest>
I think you should put
distance = location.distanceTo(locationSede); //LINE NUMBER 61
into method
#Override
public void onLocationChanged(Location location) {
}
It'll look like
#Override
public void onLocationChanged(Location location) {
if (location != null){
Location locationSede = new Location("Sede");
locationSede.setLatitude(45.012447);
locationSede.setLongitude(7.621013);
int distance = location.distanceTo(locationSede);
}
}
If you want to calculate distance between 2 points then you can use to Location.distanceBetween( A.Latitude, A.Longitude, B.Latitude, B.Longitude, result);
See below code how to impliment in your code
float[] result = new float[1];
Location.distanceBetween(location.getLatitude(), location.getLongitude(), locationSede.getLatitude(), locationSede.getLongitude(), result);
Log.e("TAG Result",""+result); //Here result is your distance
double km = result * 0.001;
Log.e("TAG Result",""+km); //Here distance in kilometer
Here when you want to distance in kilometer that time total distance with multiply 0.001 as like distancein_km=total_dis*001
I resolve my problem, the error is caused by the provider: I use locationManager.allProvider(); and in this way it works.
Thanks anyway
I cant get latitude and longitude in lollipop.It works fine in other versions like gingerbread and kitkat and maximum sdk is 22. I have given
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Java code .
package com.example.project2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.util.Log;
public class MainActivity extends Activity implements LocationListener {
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 10 sec
protected LocationManager locationManager;
protected Context context;
protected boolean gps_enabled, network_enabled;
TextView txtLat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.t1);
locationManager = (LocationManager)getSystemService(Context.Location_Service);
// getting GPS status
gps_enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
network_enabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVID ER);
if (gps_enabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
} else if (network_enabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
};
}
#Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.t1);
txtLat.setText("Latitude:"+ location.getLatitude() + ", Longitude:"
+ location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {
Log.d("Latitude", "disable");
}
#Override
public void onProviderEnabled(String provider) {
Log.d("Latitude", "enable");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras){
Log.d("Latitude", "status");
}
}
I did it in Eclipse Juno.My main intention is to get location and send to another mobile which i provided.But first I am not getting any location.
Please go through the following links.
Location issue with lollipop
Retrieve current location
It may help you. If not, let me know.
Avoid using this old method and look at Making Your App Location-Aware
I Have This Creepy problem. i am trying to get the location of my emulator. it working fine when i get the location of my emulator. but when i change my location coordinates nothing happens. gps is working fine.
Here is my code
Main.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class Main extends Activity {
TextView tvStatus;
LocationManager lm;
boolean gpsEnabled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStatus = (TextView) findViewById(R.id.textView1);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("", "GPS is Active");
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
tvStatus.setText(l.getLatitude()+" , "+l.getLongitude());
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1,
Bundle arg2) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location arg0) {
tvStatus.setText("GPS ENABLED: " + gpsEnabled
+ "\nLatitude: " + arg0.getLatitude()
+ "\nLongitude: " + arg0.getLongitude());
}
});
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.enabelinglocationprovider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="10" />
<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.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.enabelinglocationprovider.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Samsung phones have this problem. There is hack to this. You need to kick the locationmanager on the butt to get the latest location from the maps cache.
God.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(final Location location) {
}
});
currentLocation = God.locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Call getLastKnownLocation after kicking the requestLocationUpdates with 0's.
It happens with emulators, try rebooting the emulator.
Or, close both eclipse and emulator and start both again
I made an Application that shows my GPS coordinates (Langitude and Latitude) number, which designed with out a map. But I would like to send the lat and long to mobile number/phone number Automatically (no need a send button) every time I open this Application. Anyone can help me..
Gps.java
package Sample.gps.send;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class Gps extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Disabled",
Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Enabled",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
Here is for the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Sample.gps.send"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon"
android:label="#string/app_name">
<activity android:name=".UseGps"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
</manifest>
Here MyLocationListener is an inner class. Can anyone tell how to retrieve the coordinates whenever onLocationChanged method is called? The location is displayed using a Toast. Is it possible to get the coordinates by calling any method from onCreate()?
See, http://mobiforge.com/developing/story/sms-messaging-android. That tutorial will show you how to send an SMS, using that you should be able to figure out what you need to do.
Please use the code tags when posting code samples :-)
package gpstest.example.com;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.content.Context;
import android.graphics.Paint;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class gpstestActivity extends Activity
{
public LocationManager mlocManager;
public LocationListener mlocListener;
public boolean isFirstRun=true;
protected final Paint mPaint = new Paint();
private MapView myMap;
private MyLocationOverlay myLocOverlay;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gpsEnable();
setMap();
initMyLocation();
}
public void setMap(){
MapView map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
map.getController().setZoom(16);
map.getController().setCenter(new GeoPoint(30266000, -97739000));
}
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
}
public void gpsEnable (){
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 10, 50, mlocListener);
}
public void gpsDisable(){
mlocManager.removeUpdates(mlocListener);}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
double longi=loc.getLongitude();
double lati=loc.getLatitude();
MapView map = (MapView) findViewById(R.id.map);
map.getController().setCenter(new GeoPoint(lati, longi));
String Text="My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
//initMyLocation();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
mlocManager.removeUpdates(mlocListener);
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(),
"Status changed",
Toast.LENGTH_SHORT).show();
}
}/* End of Class MyLocationListener */
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.beenden: mlocManager.removeUpdates(mlocListener);}
return true;
}
protected void onPause(){
Toast.makeText( getApplicationContext(),
"On Pause",
Toast.LENGTH_SHORT).show();
gpsDisable();
isFirstRun=false;
super.onPause();
}
protected void onDestroy(){
Toast.makeText( getApplicationContext(),
"Destroyed",
Toast.LENGTH_SHORT).show();
gpsDisable();
super.onDestroy();
}
protected void onResume(){
if (isFirstRun==false){gpsEnable();}
Toast.makeText( getApplicationContext(),
"Resumed",
Toast.LENGTH_SHORT).show();
super.onResume();
}
}/* Ende Activity */
Whenever I call initMyLocation, the app crashes (bot on emulator and device). Same happens when I try to implement a MiniMap. I do exactly the same as in the SampleExtensive example from the Osmdroid repository. What am I doing wrong?
My AndroidManifest.xml is giving all required permissions I think
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".gpstestActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In your setMap(), you never set
myMap = map;
So in initMyLocation, you are passing null for the pMapView parameter.
to use location you must have the following in your AndroidManifest.xml :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>