谷歌地图和Java。我需要获取设备的long / lat然后将其放入变量中

时间:2017-02-05 11:56:55

标签: java android google-maps

目前正致力于基于位置的AR游戏。我有一些编译不起作用的代码(我正在使用Android Studio,但代码是Java)。

基本上,我是地图API的新手,我需要帮助修复错误。如何添加我的API密钥,我应该使用哪个Google位置API,以及如何将其实际放入变量?非常感谢。 (忽略,有些代码不会进入编码框,对不起。所有导入的东西都在这句话之后)。

import android.content.IntentSender;
import java.util.*;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.R;
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.OnMapReadyCallback;
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 getLocation extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
public static final String TAG = getLocation.class.getSimpleName();
private GoogleMap mMap;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST=9000;
private LocationManager locationManager;


//This is readily being processed
public double [] handleNewLocation(Location location){
        Log.d(TAG, location.toString());
        double locLatitude = location.getLatitude();
        double locLongitude = location.getLongitude();

        double [] output = {locLatitude, locLongitude};

    return output;

}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    while (true) {
        double [] coor = handleNewLocation(LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient));
        areaDefiner(coor);

    }

}

//This is done three times based on original location
public static double [] areaDefiner(double [] input) { //input coordinates
    double lat = input[0];
    double lon = input[1];
    //Creates an area
    double size = 135.5;
    double poop =  Math.random() * size;
    double pm = Math.random();
    if(pm >= 0.5) {
        poop *= - 1;
    }

    double outLat = lat + poop;
    double outLon = lon + poop;

    double [] output = {outLat, outLon};
    return output;
}

@Override
protected void onResume() {
    super.onResume();
    //setUpMapIfNeeded();
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    super.onPause();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}




@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.i(TAG, "Location services connected.");
    try {

        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if (location == null){}
        else{
            handleNewLocation(location);
        }

    } catch(SecurityException e) { // HAVE NO IDEA IF THIS IS RIGHT
        e.printStackTrace();
    }

}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "Location services suspended. Please reconnect.");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    } else {
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

}

0 个答案:

没有答案