解析经度和纬度以在谷歌地图上再次使用它[关闭]

Posted

技术标签:

【中文标题】解析经度和纬度以在谷歌地图上再次使用它[关闭]【英文标题】:parse langtude and latitude to use it again on google map [closed] 【发布时间】:2016-03-22 07:59:53 【问题描述】:
@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.txt);


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    
    locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, this);
    locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, this);



@Override
public void onLocationChanged(Location location) 
    txt=(TextView)findViewById(R.id.txt);
    txt.setText("longtude is :" + location.getLongitude() + "latitude is :" + location.getLatitude());


@Override
public void onStatusChanged(String provider, int status, Bundle extras) 



@Override
public void onProviderEnabled(String provider) 



@Override
public void onProviderDisabled(String provider) 


【问题讨论】:

【参考方案1】:

检查清单中的给定权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

在 onCreate 中添加以下行

Location l;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt = (TextView) findViewById(R.id.txt);


locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;

locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, this);
Criteria c=new Criteria();
provider=locationManager.getBestProvider(c, false);
l=locationManager.getLastKnownLocation(provider);
if(l!=null)

 //get latitude and longitude of the location
 double lng=l.getLongitude();
 double lat=l.getLatitude();
 //display on text view
 txt.setText(lng+ ""+ lat);

else

 txt.setText("No Provider");



@Override
public void onLocationChanged(Location arg0)

 double lng=l.getLongitude();
 double lat=l.getLatitude();
 txt.setText(lng+ ""+ lat);

【讨论】:

谢谢,它对我有用,但经度和纬度仅出现在 ddms 上如何在我的活动中显示这些值@pavan kvch

以上是关于解析经度和纬度以在谷歌地图上再次使用它[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

我想通过从 json 获取经度和纬度在谷歌地图上显示标记

如何在谷歌地图API中获取高速公路和道路的纬度和经度

如何通过对vue js中的每个搜索结果使用模态在谷歌地图上显示json数据获得的纬度和经度?

在android中将坐标传递给谷歌地图

使用纬度和经度显示谷歌地图

在谷歌地图 api v3 中将地区、城市、州、国家/地区转换为纬度和经度?