I am using eclipse. Source code is given below : When I click first button on various places, the result is same. Please help.. Thanks in advance
GPSSurvey Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vinod.gpssurvey"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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.vinod.gpssurvey.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>
MainActivity.java
package com.vinod.gpssurvey;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSend = (Button) findViewById(R.id.Firstbutton);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
//locationManager.requestLocationUpdates(bestProvider, 400, 1, this);
Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat,lon;
try {
lat = location.getLatitude ();
lon = location.getLongitude ();
TextView firstLat = (TextView)findViewById(R.id.FirstLat);
TextView firstLong = (TextView)findViewById(R.id.FirstLong);
firstLat.setText(lat.toString());
firstLong.setText(lon.toString());
String Text = "My current location is: " +
"Latitud = " + location.getLatitude() +
"Longitud = " + location.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
catch (NullPointerException e){
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/FirstLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Longitute : " />
<TextView
android:id="@+id/FirstLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Latitute : " />
<Button
android:id="@+id/Firstbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First Corner" />
<TextView
android:id="@+id/SecLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sec Longitute : " />
<TextView
android:id="@+id/SecLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sec Latitute : " />
<Button
android:id="@+id/Firstbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Second Corner" />
<TextView
android:id="@+id/ThirdLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Longitute : " />
<TextView
android:id="@+id/ThirdLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Latitute : " />
<Button
android:id="@+id/Thirdbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Third Corner" />
<TextView
android:id="@+id/FourthLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fourth Longitute : " />
<TextView
android:id="@+id/FourthLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fourth Latitute : " />
<Button
android:id="@+id/Fourthtbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fourth Corner" />
<TextView
android:id="@+id/FifthLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fifth Longitute : " />
<TextView
android:id="@+id/FifthtLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fifth Latitute : " />
<Button
android:id="@+id/Fifthbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fifth Corner" />
</LinearLayout>
The call to getLastKnownLocation
gives you the last location detected, when the LocationProvider (usually GPS or WiFi) was active and has actually detected a location.
Depending on your device, it can take a while (like 1 minute or longer) till the GPS provider delivers the first location. You can see the status of connecting to GPS satellites on the navigation bar (again, depending on the device you use)
If you're indoors it might never receive any locations.
Your code looks ok to me.
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.