Google Location api不显示坐标

时间:2015-07-09 04:35:21

标签: java android

我正在使用Google Play服务来获取用户的当前位置,并尝试在TextView中显示它,但由于某种原因没有发生任何事情。没有日志打印出来,活动仍然说“hello world”而不是坐标。该应用程序也符合并运行,没有错误。

编辑:如果有帮助,位置图标会在我的通知面板上显示几秒钟。

func spawnEnemy1() {
        var enemy1Texture = SKTexture(imageNamed: "enemy1")

        enemy1 = SKSpriteNode(texture: enemy1Texture)
        enemy1.setScale(0.3)
        enemy1.name = "enemy1"

        enemy1.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 1.2)
        enemy1.zPosition = 55

        var enemyPhysicsSize = CGSize(width: enemy1.size.width,height: 65)
        enemy1.physicsBody = SKPhysicsBody(rectangleOfSize: enemyPhysicsSize)
        enemy1.physicsBody?.dynamic = false

        enemy1.physicsBody?.categoryBitMask = ColliderType.Enemy1.rawValue
        enemy1.physicsBody?.contactTestBitMask = ColliderType.Savior.rawValue
        enemy1.physicsBody?.collisionBitMask = ColliderType.Savior.rawValue

        self.addChild(enemy1)

        let enemyMoveDown = SKAction.moveTo(CGPointMake(self.size.width * 0.5,-100), duration:5.6)

        let scaleEnemy = SKAction.scaleTo(0.5, duration: 5.6)

        let enemyScaleDown = SKAction.group([enemyMoveDown, scaleEnemy])

        enemy1.runAction(enemyScaleDown)
    }

2 个答案:

答案 0 :(得分:2)

您似乎忘了拨打mGoogleApiClient.connect(),也从不致电requestLocationUpdates()

以下是我测试过的代码并且有效:

public class MainActivity extends ActionBarActivity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private TextView locationText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationText = (TextView) findViewById(R.id.location);

        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause(){
        super.onPause();
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        //mLocationRequest.setSmallestDisplacement(0.1F);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {

        //Set contents of your TextView here
        locationText.setText("lat: " + location.getLatitude() + " lon: " + location.getLongitude());

        //set last location member variable
        mLastLocation = location;

        Log.d("locationtesting", "accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude());

        Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show();
    }
}

答案 1 :(得分:-1)

public class LocationFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationListFragment.class.getSimpleName();

private Location mLastLocation;

/**
 * Provides the entry point to Google Play services.
 */
protected GoogleApiClient mGoogleApiClient;

private FloatingActionButton fab;

private boolean mIsConnected = false;

@Override
public void onConnected(Bundle bundle) {
     fab.setVisibility(View.VISIBLE);
     mIsConnected = true;
}

@Override
public void onConnectionSuspended(int i) {
    fab.setVisibility(View.INVISIBLE);
    mIsConnected = false;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public LocationFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main, container, false);
    fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getLastLocation();
        }
    });

    buildGoogleApiClient();
    return rootView;
}

private void getLastLocation() {
    if(mIsConnected) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(mLastLocation != null) {
            //TODO: do something with the location.
        }
    }

}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}


@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();
}

@Override
public void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
}

使用融合位置提供程序获取最后位置的示例。这是来自具有材料设计元素的GoogleMaps的示例。单击浮动操作按钮时,片段将获取最后一个位置。请记住@Daniel Nugent提到的关于getLastLocation()调用可靠性的内容。虽然我认为它没用,但确实有其局限性。对于实时更新,请注册Daniel Nugent的答案。 GitHub Link to above code