I'm studying programming for Andriod and I came across a feature that I just do not know anything about, which is ordering the functions ! please could anyone give a light?
I explain, in my example I call
- API to communicate with GPS, (line 90)
- AFTER to effectively pick up the position (latitude and longitude) (line 103)
- and then I call the map ! (line 210)
But I do not know why, it opens the map BEFORE you pick up the GPS position, that is, the map has no open position and error.
As a newcomer, I do not know exactly if I was clear ... I hope you help me understand and explain. I got here until after a lot of search, but I packed, because I do not know how to open the map AFTER the query of position ...
Here is the code I'm working on !
package jp.co.e_grid.rakuseki;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
//import java.util.concurrent.atomic.DoubleAccumulator;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import jp.co.e_grid.rakuseki.model.Report;
import static jp.co.e_grid.rakuseki.config.Constants.MAP_ZOOM;
/**
* Created by ootaegd on 2016/12/16.
*/
public class PostPositionActivity extends AppCompatActivity
implements OnMapReadyCallback, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleMap map;
private Marker marker = null;
private LatLng locationLatLng;
//private static final LatLng MATSUE = new LatLng(35.47222, 133.05056); //マーカ初期値
//private LatLng locationLatLng = instantPosition;
private float accuracy;
//private static final LatLng instantPosition = new LatLng(35.3852243, 132.7339911); // izumo
//private static final LatLng instantPosition = new LatLng(35.47222, 133.05056); //マーカ初期値 matsue
private String uuId;
//private LatLng instantPosition;
protected Realm realm;
private TextView tvCoordinate;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_position);
//前のactivityから渡されたデータを取得する
Intent intent = getIntent();
uuId = intent.getStringExtra("uuId");
//realm設定
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
//toolbarを設定
Toolbar toolbar = (Toolbar) findViewById(R.id.post_position_toolbar);
toolbar.setTitle(getString(R.string.headPositionTitle));
setSupportActionBar(toolbar);
onBtnGpsClicked();
nextViewActivity();
//Fragment を取得
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
tvCoordinate = (TextView) findViewById(R.id.tv_coordinate);
callConnection();
}
/**
* Getting the Google GPS API OnLine.
*(linha 90)
*/
private synchronized void callConnection() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
Log.e("--------MESSAGE--------"," API loaded 01");
}
/**
* Getting the actual GPS position.
*(linha 103)
*/
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location l = LocationServices
.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if(l != null){
//tvCoordinate.setText(l.getLatitude()+" -- "+l.getLongitude()+"-----"+ l.getAccuracy());
tvCoordinate.setText("Accuracy = "+ l.getAccuracy());
accuracy = l.getAccuracy();
Log.e("--------LOG--------", "latitude: " + l.getLatitude());
Log.e("--------LOG--------", "longitude: " + l.getLongitude());
Log.e("--------LOG--------", "accuracy: " + accuracy);
// tvCoordinate.setText(" ||| "+l.getLatitude()+" ||| "+l.getLongitude()+" ||| ");
// final LatLng instantPosition = new LatLng(35.3852243, 132.7339911); //izumo
// final LatLng instantPosition = new LatLng(35.47222, 133.05056); // matsue
locationLatLng = new LatLng(l.getLatitude(), l.getLongitude());
//instantPosition = new LatLng(l.getLatitude(), l.getLongitude());
Log.e("--------MESSAGE--------"," GPS get ok 02");
}
}
@Override
public void onConnectionSuspended(int i) {
}
/**
* 次の画面へ遷移する処理
*/
private void nextViewActivity() {
//位置情報ボタンを押された時の処理
Button btnPost = (Button) findViewById(R.id.btnPost);
btnPost.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//位置情報を追加する
addReport();
// 画面を起動
Intent intent = new Intent();
intent.setClassName("jp.co.e_grid.rakuseki", "jp.co.e_grid.rakuseki.PostConfirmationActivity");
intent.putExtra("uuId",uuId);
startActivity(intent);
}
});
}
/**
* 現在地取得ボタンを押された時、現在地を取得する
*/
private void onBtnGpsClicked() {
Button btnPost = (Button) findViewById(R.id.btnLocation);
btnPost.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//現在地取得
}
});
}
/**
* GoogleMapを読み込む前に、オーバライドして処理を行う
*
* @param googleMap
*(linha 210)
*/
@Override
public void onMapReady( GoogleMap googleMap ) {
map = googleMap;
Log.e("--------MESSAGE--------"," original get 03");
// final LatLng instantPosition = new LatLng(35.3852243, 132.7339911); // izumo
// final LatLng instantPosition = new LatLng(35.47222, 133.05056); // matsue
//locationLatLng = new LatLng(35.47222, 133.05056); // matsue //test
setMarker(locationLatLng);
// GoogleMapが押下された時の処理
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick( LatLng latLng ){
// クリックされるたびにマーカが増えていく
// 一つ前のマーカは削除する
marker.remove();
//Mapが押下されたらその位置にピンを立てる
//緯度経度を取得
locationLatLng = latLng;
//ピンを立てる
setMarker(locationLatLng);
}
});
}
/**
* Markerを立てる関数
*
* @param lacation 緯度経度情報
*/
private void setMarker(LatLng lacation){
marker = map.addMarker(new MarkerOptions()
.position(lacation)
.title("報告場所")
.draggable(false));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(lacation, MAP_ZOOM));
}
/**
* 位置情報を追加する
* @return プライマリキー
*/
private void addReport(){
realm = Realm.getDefaultInstance();
//トランザクション開始
realm.beginTransaction();
//uuIdでターゲットを抽出
Report report = realm.where(Report.class).equalTo("key",uuId).findFirst();
//保存を行う
report.setLat(Double.parseDouble(String.valueOf(locationLatLng.latitude)));
report.setLon(Double.parseDouble(String.valueOf(locationLatLng.longitude)));
report.setAccuracy(Double.parseDouble(String.valueOf(accuracy)));
Log.e("--------MESSAGE--------", " Longitude 04 " + String.valueOf(report.getLon()));
Log.e("--------MESSAGE--------", " Latitude 04 " + String.valueOf(report.getLat()));
Log.e("--------MESSAGE--------", " Accuracy 04 " + String.valueOf(report.getAccuracy()));
//トランザクション終了
realm.commitTransaction();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}