gpt4 book ai didi

java - 无法从此代码接收位置坐标?

转载 作者:行者123 更新时间:2023-12-01 10:36:05 32 4
gpt4 key购买 nike

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.talk" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >


<activity
android:name=".MainAppActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".Login">
</activity>

<activity android:name=".UserRegistration">
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyC9QWvezlYxdZcmFLjUlRND44-g9Ardcp0"/>
</application>
<service android:name=".GeofenceTransitionsIntentService"/>

LocationReceiver.java

package com.mycompany.talk;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

/**
* Created by joshua on 1/10/2016.
*/
public class LocationReceiver extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private LocationListener mLocationListener;
private LocationManager mLocationManager;
private static double mLatitudeText;
private static double mLongitudeText;
private int radius = 100;
private Location mLastLocation;
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

我为此引用了 Android 开发人员指南。

    if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}

protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}

private void setLat(double latitude) {
this.mLatitudeText = latitude;
}


private void setLon(double longitude) {
this.mLongitudeText = longitude;

}

@Override
public void onConnectionSuspended(int x){

}

当我从 MapFragment.java 调用 getter 时,将使用我的默认坐标而不是我应该接收的坐标。

@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
setLat(mLastLocation.getLatitude());
setLon(mLastLocation.getLongitude());
}
}

@Override
public void onConnectionFailed(ConnectionResult CR){

}

public static double getLat() {
return mLatitudeText;
}

public static double getLon() {
return mLongitudeText;
}
}

MapFragment.java

    package com.mycompany.talk.Fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.api.GoogleApiClient;
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;
import com.mycompany.talk.LocationReceiver;
import com.mycompany.talk.R;

public class MapFragment extends Fragment implements OnMapReadyCallback{
SupportMapFragment mSupportMapFragment;

LocationReceiver LR;
GoogleApiClient mGoogleApiClient;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mSupportMapFragment = SupportMapFragment.newInstance();
android.support.v4.app.FragmentManager sfm = getFragmentManager();
mSupportMapFragment.getMapAsync(this);
if(!mSupportMapFragment.isAdded())
sfm.beginTransaction().add(R.id.map_frag,mSupportMapFragment).commit();

else if(mSupportMapFragment.isAdded())
sfm.beginTransaction().hide(mSupportMapFragment).commit();
else
sfm.beginTransaction().show(mSupportMapFragment).commit();

return inflater.inflate(R.layout.fragment_map, container, false);
}

@Override
public void onMapReady(GoogleMap map) {
LR = new LocationReceiver();
if (LR.getLat() != 0 && LR.getLon() != 0) {
LatLng location = new LatLng(LR.getLat(), LR.getLon());
map.addMarker(new MarkerOptions().position(location).title("You"));
map.moveCamera(CameraUpdateFactory.newLatLng(location));
}
else{
LatLng location = new LatLng(32.689576, -117.115744);
map.addMarker(new MarkerOptions().position(location).title("You"));
map.moveCamera(CameraUpdateFactory.newLatLng(location));
}
}
}

最佳答案

在您请求位置更新之前,您将不会收到位置信息。您可以相应地设置请求值以节省电池生命周期。

@Override
protected void onCreate(Bundle savedInstanceState) {
...
createLocationRequest();
}

protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onConnected(Bundle connectionHint) {
...
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}

关于java - 无法从此代码接收位置坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713666/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com