问题描述
- android无法显示地理位置信息,求帮忙看下代码
-
package com.example.locationtest; import java.util.List; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView positionTextView; private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); positionTextView=(TextView)findViewById(R.id.position_text_view); locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); List<String> providerList=locationManager.getProviders(true); if(providerList.contains(LocationManager.GPS_PROVIDER)) { provider=LocationManager.GPS_PROVIDER; } else if(providerList.contains(LocationManager.NETWORK_PROVIDER)) { provider=LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show(); return; } Location location=locationManager.getLastKnownLocation(provider); if(location != null) { showLocation(location); } locationManager.requestLocationUpdates(provider,5000, 1, locationListener); } protected void onDestroy() { super.onDestroy(); if(locationManager != null) { locationManager.removeUpdates(locationListener); } } LocationListener locationListener=new LocationListener() { @Override public void onStatusChanged(String provider,int status,Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(Location location) { showLocation(location); } }; private void showLocation(Location location) { String currentPosition="latitude is"+location.getLatitude()+"n"+"longitude is"+location.getLongitude(); positionTextView.setText(currentPosition); } }
解决方案
你配置文件中有没有加权限?
解决方案二:
http://blog.csdn.net/vnanyesheshou/article/details/49924237
解决方案三:
http://blog.csdn.net/octobershiner/article/details/6628241
参考。
解决方案四:
1.可能没加权限
2.可能没联网也没开GPS
3.可能你把权限关了
解决方案五:
使用百度地图吧。http://blog.csdn.net/column/details/android-jacksen-map.html
时间: 2024-12-03 12:13:35