I create apps to get user locations in coordinate like latitude and longitude, so I use play-service-location, my program code is below:
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Location mLastLocation;
private Button btLocation;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupGoogleAPI();
// initialize button
btLocation = (Button) findViewById(R.id.bt_getLocation);
btLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mLastLocation != null) {
Toast.makeText(MainActivity.this," Get Location \n " +
"Latitude : "+ mLastLocation.getLatitude()+
"\nLongitude : "+mLastLocation.getLongitude(), Toast.LENGTH_LONG).show();
}
}
});
}
private void setupGoogleAPI(){
// initialize Google API Client
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
// connect ke Google API Client ketika start
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
// disconnect ke Google API Client ketika activity stopped
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
// get last location ketika berhasil connect
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
Toast.makeText(this," Connected to Google Location API", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
as seen above, I use toast to display the coordinates, the problem is when I experiment with adding setText to display coordinates, there is an error
error: no suitable method found for setText(double) method TextView.setText(CharSequence) is not applicable (argument mismatch; double cannot be converted to CharSequence) method TextView.setText(int) is not applicable (argument mismatch; possible lossy conversion from double to int
I have tried several days and the result is always error, how I display the coordinates in setText, so the coordinates can be seen in textView?