Android 如何检索当前设备位置&;在片段中的地图片段上显示它
我正在用谷歌地图开发一个android应用程序。目前我可以在我的应用程序中查看地图,但我不知道如何在应用程序上查看当前位置 这是我的密码:Android 如何检索当前设备位置&;在片段中的地图片段上显示它,android,google-maps,android-layout,android-mapview,google-maps-android-api-2,Android,Google Maps,Android Layout,Android Mapview,Google Maps Android Api 2,我正在用谷歌地图开发一个android应用程序。目前我可以在我的应用程序中查看地图,但我不知道如何在应用程序上查看当前位置 这是我的密码: public class MapsFragment extends Fragment{ MapView m; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bu
public class MapsFragment extends Fragment{
MapView m;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.map_near_me, container, false);
m = (MapView) v.findViewById(R.id.map);
m.onCreate(savedInstanceState);
return v;
}
}
编辑:
以及xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</LinearLayout>
这段代码运行良好,我知道“setmylocationenabled”有助于在FragmentActivity中启用,但不幸的是,我不得不将该类型用作“Fragment”。我使用的是谷歌api v2。请找人帮忙。您可以启用您的位置,只需在类中添加此代码即可
GoogleMap.setMyLocationEnabled(true);
这是GP斯特拉克的课程
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
//
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 50; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
//getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Constant.mLocation = location;
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Constant.mLocation = location;
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS settings");
// Setting Dialog Message
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public void onLocationChanged(Location location) {
Constant.mLocation = location;
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
您应该像这样初始化Map v2:
m = (MapView) v.findViewById(R.id.map);
GoogleMap mMap = m.getMap();
现在您可以使用
mMap.setMyLocationEnabled(true)代码>这里的链接解释了您需要的所有内容
要在地图上设置位置,您需要使用下面的类
LatLong objLatLng=new LatLong(lat,longi);
yourMapObject.moveCamera(CameraUpdateFactory.newLatLngZoom(objLatLng, 20));
yourMapObject.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);
希望这将对您有所帮助。使用新推出的融合定位提供商如何
引用自:
使用XML作为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp"
android:layout_weight="1" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
</RelativeLayout>
如果您不具备所有要求,可能会得到一张空白地图,
通过以下帖子获得项目的Play服务
然后获取api密钥:
将权限添加到您的清单中
<uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
要测试地图应用程序,您需要一个真实的设备,如果没有,则通过adb将播放服务推送到emulator,阅读本文了解如何通过adb安装播放服务
完成上述所有步骤后,清理项目,从emulator卸载先前的.apk,然后运行项目
我们需要你的谷歌地图代码!!你从哪里得到地图来管理它?您应该使用SupportMapFragment,使用support lib v4@Yume117:犯了一个错误检查我的xml代码而不是MapView使用SupportMapFragment或Fragment Map:)所以你使用Google Map api v1?因为这种显示地图的方法不再被推荐了:/所以我如何在类类型片段中实现它?尝试过,但强制关闭。CatLog:08-30 13:26:48.635:D/AndroidRuntime(11455):关闭VM 08-30 13:26:48.635:W/dalvikvm(11455):threadid=1:线程以未捕获异常退出(组=0x4164a2a0)08-30 13:26:48.635:E/AndroidRuntime(11455):致命异常:main 08-30 13:26:48.635:E/AndroidRuntime(11455):java.lang.ClassCastException:android.widget.FrameLayout无法强制转换为com.google.android.gms.maps.mapView这些是可以根据需要使用的常量变量。mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(新tLng(纬度,经度),0));请同时提供您的xml。Get-SupportFragmentManager()上的Get-redflag“类型活动的getSupportFragmentManager()方法未定义”我编辑了代码,尝试使用getFragmentManager()
,将所有SupportMapFragment
更改为MapFragment
,在yur xml中,也将其从SupportMapFragments更改为MapFragment抱歉,回复太晚。但是当我运行它的时候,我得到了一个空白的白色屏幕,在我的日志中:09-01 13:01:02.760:E/GooglePlayServicesUtil(12622):没有找到GooglePlayServices资源。检查您的项目配置以确保包含资源。但我在清单中添加了所有必需的权限。感谢您的回答,我遵循了您所说的所有步骤,并在真实设备上进行了测试。但是仍然是相同的结果,上面的代码(我发布的)正确地显示了地图。我认为应该还有一些其他问题。需要更多的帮助。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp"
android:layout_weight="1" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
</RelativeLayout>
<uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />