我已经看到了关于这个问题的帖子很多,但没有人提供了一个解决方案给我。
我想获得通过GPS(或最终,任何其他有效的提供商)用户的位置。但是,我总是得到空的位置。我得到一张地图,并且可以设置任意的位置,是不工作的提供者。
我有我的地图密钥设置正确(还要检查,这是正确的),将所有必要的权限( android.permission.INTERNET对
, android.permission.ACCESS_COARSE_LOCATION
, android.permission.ACCESS_FINE_LOCATION
),而事实上,似乎都只是工作昨天罚款!
我想获得所有潜在供应商提供这个code:
列表<串GT;提供商= locManager.getAllProviders();对于(字符串提供商:提供商){ printProvider(供应商);}
用于显示所有供应商(4,在我的情况)是DummyLocationProviders。
我想在手机上运行我的应用程序的,而不是模拟器(虽然我也用模拟器试过,启用了GPS,但没有成功)。
这是code我使用获取的位置(这又工作得很好昨天):
//获取标准 标准标准=新标准(); criteria.setAccuracy(Criteria.ACCURACY_FINE); //获取最佳供应商 字符串bestProvider = locManager.getBestProvider(标准,FALSE); printProvider(bestProvider); //从位置管理器获取当前位置(最近已知位置) 地点= locManager.getLastKnownLocation(bestProvider); //如果发现地点显示为敬酒当前的纬度和经度 如果(位置!= NULL){ Toast.makeText(这一点,您现在的位置:\\ nLatitude:+ location.getLatitude()+\\ n+经度:+ location.getLongitude(),Toast.LENGTH_LONG).show(); 点=新的GeoPoint((INT)(location.getLatitude()* 1E6),(INT)(location.getLongitude()* 1E6)); controller.animateTo(点); }其他{ Toast.makeText(这一点,无法获取当前位置!,Toast.LENGTH_LONG).show(); }
我的清单文件:
<采用-SDK 安卓的minSdkVersion =10 机器人:targetSdkVersion =10/> <使用许可权的android:NAME =android.permission.INTERNET对/> <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/> <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/> <应用 机器人:图标=@绘制/ ic_launcher 机器人:标签=@字符串/ APP_NAME 机器人:主题=@风格/ AppTheme> <活动 机器人:名字=。activities.MainActivity 机器人:标签=@字符串/ title_activity_main> &所述;意图滤光器> <作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/> &所述; /意图滤光器> < /活性GT; <使用库机器人:名字=com.google.android.maps/> <活动 机器人:名字=。activities.GoogleMapsActivity 机器人:标签=@字符串/ title_activity_maps> < /活性GT; <活动 机器人:名字=。activities.MyMapsActivity 机器人:标签=@字符串/ title_activity_maps> < /活性GT;< /用途>< /清单>
解决方案
试试这个code:
进口java.io.IOException异常; 进口的java.util.List; 进口java.util.Locale中; 进口android.content.Context; 进口android.graphics.Canvas; 进口android.location.Address; 进口android.location.Geo codeR; 进口android.location.Location; 进口android.location.LocationListener; 进口android.location.LocationManager; 进口android.os.Bundle; 进口android.widget.Toast; 进口com.google.android.maps.GeoPoint; 进口com.google.android.maps.MapActivity; 进口com.google.android.maps.MapController; 进口com.google.android.maps.MapView; 进口com.google.android.maps.Overlay; 公共类MyMapsActivity扩展MapActivity { MapView类MapView类; MapController mapController; 的LocationManager的LocationManager; LocationListener的LocationListener的; / **当第一次创建活动调用。 * / @覆盖 公共无效的onCreate(捆绑savedInstanceState) { super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 图形页面=(图形页面)findViewById(R.id.mapView); //默认启用街景 mapView.setStreetView(真); //启用显示卫星视图 // mapView.setSatellite(真); //启用,以在地图上显示交通 // mapView.setTraffic(真); mapView.setBuiltInZoomControls(真); mapController = mapView.getController(); mapController.setZoom(5); 的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener的=新GPSLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0,0,LocationListener的); 敏感的T =新暴躁(); 清单<&叠加GT; overlayList =调用MapView.getOverlays(); overlayList.add(T); } @覆盖 保护布尔isRouteDisplayed(){ // TODO自动生成方法存根 返回false; } 类暴躁扩展覆盖 { 公共布尔中的onTap(GeoPoint的点,MapView类MapView类) { 上下文contexto = mapView.getContext(); 弦乐味精=纬度:+ point.getLatitudeE6()/ 1E6 + - +经度:+ point.getLongitudeE6()/ 1E6; 吐司面包= Toast.makeText(contexto,味精,Toast.LENGTH_SHORT); toast.show(); 返回true; } } 私有类GPSLocationListener实现LocationListener的 { 公共无效onLocationChanged(地点) { 如果(位置!= NULL) { 的GeoPoint点=新的GeoPoint((INT)(location.getLatitude()* 1E6),(INT)(location.getLongitude()* 1E6)); Toast.makeText(getBaseContext(),纬度:+ location.getLatitude()+经度:+ location.getLongitude(), Toast.LENGTH_SHORT).show(); mapController.animateTo(点); mapController.setZoom(5); mapView.invalidate(); } 如果(位置!= NULL) { 的GeoPoint点= NULL; 字符串的地址= ConvertPointToLocation(点); Toast.makeText(getBaseContext(),地址,Toast.LENGTH_SHORT).show(); } } 公共字符串ConvertPointToLocation(GeoPoint的点){ 字符串的地址=; 地理codeR地理codeR =新的地缘codeR(getBaseContext(),Locale.getDefault()); 尝试{ 清单<地址>地址=地理coder.getFromLocation(point.getLatitudeE6()/ 1E6, point.getLongitudeE6()/ 1E6,1); 如果(addresses.size()大于0){ 对(INT索引= 0; 指数< addresses.get(0).getMaxAddressLineIndex();指数++) 地址+ = addresses.get(0).getAddressLine(指数)+; } } 赶上(IOException异常五){ e.printStackTrace(); } 退货地址; }
}
布局编码:
<?XML版本=1.0编码=UTF-8&GT?; < RelativeLayout的 的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =FILL_PARENT 机器人:layout_height =FILL_PARENT> < com.google.android.maps.MapView 机器人:ID =@ + ID /图形页面 机器人:layout_width =FILL_PARENT 机器人:layout_height =FILL_PARENT 机器人:启用=真 机器人:可点击=真 机器人:apiKey =你的地图API密钥/> <的LinearLayout机器人:ID =@ + ID /缩放 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignParentBottom =真 机器人:layout_centerHorizontal =真/> < / RelativeLayout的>
链接获取自己的API密钥的过程:
的http://sanathnandasiri.blogspot.in/2011/04/obtaining-google-maps-api-key-for.html
I have seen many posts regarding this problem, but none of them offered a solution for me.
I am trying to get the user position through the GPS (or, ultimately, any other valid provider). But I am always getting null locations. I do get a map, and can set an arbitrary location, is the providers that don't work.
I have my MAPS key properly set (also checked that it was the right one), all necessary permissions set (android.permission.INTERNET
, android.permission.ACCESS_COARSE_LOCATION
, android.permission.ACCESS_FINE_LOCATION
) and in fact, all seemed to be working just fine yesterday!
I tried getting all possible providers with this code:
List<String> providers = locManager.getAllProviders();
for (String provider : providers) {
printProvider(provider);
}
Which shows all providers (4, in my case) are DummyLocationProviders.
I am trying to run my app on the phone, not the emulator (although I also tried with the emulator, with GPS enabled, to no avail).
This is the code I am using to fetch the location (which, again, worked just fine yesterday):
//Get criteria
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//Get best provider
String bestProvider = locManager.getBestProvider(criteria, false);
printProvider(bestProvider);
//get the current location (last known location) from the location manager
Location location = locManager.getLastKnownLocation(bestProvider);
//if location found display as a toast the current latitude and longitude
if (location != null) {
Toast.makeText(this, "Current location:\nLatitude: " + location.getLatitude() + "\n" + "Longitude: " + location.getLongitude(), Toast.LENGTH_LONG).show();
point = new GeoPoint((int) (location.getLatitude()*1E6),(int)(location.getLongitude() *1E6));
controller.animateTo(point);
} else {
Toast.makeText(this, "Cannot fetch current location!", Toast.LENGTH_LONG).show();
}
My manifest file:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activities.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".activities.GoogleMapsActivity"
android:label="@string/title_activity_maps" >
</activity>
<activity
android:name=".activities.MyMapsActivity"
android:label="@string/title_activity_maps" >
</activity>
</application>
</manifest>
解决方案
Try this code :
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Canvas;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class MyMapsActivity extends MapActivity
{
MapView mapView;
MapController mapController;
LocationManager locationManager;
LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
// enable Street view by default
mapView.setStreetView(true);
// enable to show Satellite view
// mapView.setSatellite(true);
// enable to show Traffic on map
// mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(5);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Touchy t = new Touchy();
List<Overlay> overlayList = mapView.getOverlays();
overlayList.add(t);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class Touchy extends Overlay
{
public boolean onTap(GeoPoint point, MapView mapView)
{
Context contexto = mapView.getContext();
String msg = "Latitude : " + point.getLatitudeE6()/1E6 + " - " + "Longitude : " + point.getLongitudeE6()/1E6;
Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
toast.show();
return true;
}
}
private class GPSLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
if (location != null)
{
GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
mapController.animateTo(point);
mapController.setZoom(5);
mapView.invalidate();
}
if (location != null)
{
GeoPoint point=null;
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
}
}
public String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0;
index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}
}
Layout Coding :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="Your MAP API Key"/>
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Link for getting own API key process :
http://sanathnandasiri.blogspot.in/2011/04/obtaining-google-maps-api-key-for.html