获取当前坐标的Andr​​oid [英] Get current coordinates Android

查看:107
本文介绍了获取当前坐标的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班的 MainActivity.java (主类)及 MyCurrentLoctionListener.java

I have two classes MainActivity.java (Main Class) & MyCurrentLoctionListener.java

我愿意当我运行我的应用程序,让我的当前地理位置(的onLoad-的onCreate):

I am willing to get my current geo location when I run my app (onLoad-onCreate) :

我得到嗨::空

即使我定义为myLocation公开,所以MainActivity.java(主类),可以看到它。

MainActivity.java:

package com.example.test;

import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

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

Button btn1 = (Button) findViewById(R.id.make_folder);
TextView txt1 = (TextView) findViewById(R.id.textView1);

//import android.location.LocationManager;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);    

txt1.setText("HI :: " + locationListener.myLocation);

btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {   
}           
}); //setOnClickListener    
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

MyCurrentLoctionListener.java:

package com.example.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MyCurrentLoctionListener implements LocationListener {

public String myLocation;

@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();



myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}

权限已经包含在的Manifest.xml

推荐答案

当你开始你的活动,你的MyCurrentLoctionListener将无法得到位置的时候了。

When you start your activity, your MyCurrentLoctionListener will not be able to get the location right away.

这将是最好的TextView的传递到你的MyCurrentLoctionListener类是这样的:

It would be best to pass the TextView into your MyCurrentLoctionListener class like this:

public class MyCurrentLoctionListener implements LocationListener {

public String myLocation;
private TextView mTextView;

MyCurrentLoctionListener(TextView tv) {
    this.mTextView = tv;    
}

@Override
public void onLocationChanged(Location location) {
    location.getLatitude();
    location.getLongitude();

    mTextView.setText("Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude());

}

....

这篇关于获取当前坐标的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆