I can’t open the map in my position

Asked

Viewed 82 times

2

After a lot of research, I still have difficulties with java, I’m new and I’m trying to learn. Please forgive me if I can’t ask a clear question, because I may not even know what I’m talking about,

GOOD, For example the part of maps that I can already open, but with a position of "example"

and for that I am using the following code:

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{

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

    }

    /**
     * 次の画面へ遷移する処理
     */
    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
     */
    @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) {

    }
}

How can I open this map already at my location? Turns out I picked up a lot of examples on Google, but I just can’t seem to make anything fit...

Thank you for your help and good will!!

1 answer

2


I used this lib in my project. It made it very easy.

smart-Location-lib

It is in English. If you have difficulty, use the translator

Google translator

Follow the instructions and return here with your questions.

You can also use Google’s Fused Location API, but it’s more laborious. Tutorial Fused Location API

Browser other questions tagged

You are not signed in. Login or sign up in order to post.