所有我有简单的Android应用程序来查找当前位置,我已经尝试了一些代码sbelow,但它不工作。它只显示我的静态标记,它没有显示我的当前位置标记,所以请建议我为它:
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
main.java
package com.example.mapannotations;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements LocationListener {
static final LatLng PointA = new LatLng(23.063905, 72.549629);
static final LatLng PointB = new LatLng(23.043652, 72.528048);
static final LatLng PointC = new LatLng(23.008738, 72.561865);
static final LatLng PointD = new LatLng(23.007158, 72.493887);
static final LatLng PointE = new LatLng(22.961644, 72.427111);
private GoogleMap map;
protected LocationListener locationListener;
protected Context context;
protected String latitude, longitude;
protected boolean gps_enabled, network_enabled;
public LocationManager loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
// .getMap();
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// FOR CURRENT LOCATION....
loc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
loc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
// MAP MARKERS
Marker sha = map.addMarker(new MarkerOptions().position(PointA)
.title("ShastriNagar BRTS").snippet("BRTS Stop"));
Marker nar = map.addMarker(new MarkerOptions().position(PointB)
.title("Laldarwaja").snippet("Laldarwaja AMTS Bus Depot"));
Marker pal = map.addMarker(new MarkerOptions().position(PointC)
.title("Paldi").snippet("Paldi Bus Stop"));
Marker Kar = map.addMarker(new MarkerOptions().position(PointD)
.title("Karnavati Club")
.snippet("Karnavati club- Enjoyment centre"));
Marker jiv = map.addMarker(new MarkerOptions().position(PointE).title(
"Jirvaraj Park"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(PointA, 12));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// current Location marker
Marker cur = map.addMarker(new MarkerOptions().position(
new LatLng(location.getLatitude(), location.getLongitude()))
.title("My current Location"));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.d("Latitude", "disable");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.d("Latitude", "enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.d("Latitude", "status");
}
}
的Manifest.xml
<uses-permission android:name="com.example.mapannotations.permission.MAPS_RECEIVE" />
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
请帮助我......谢谢
答案 0 :(得分:3)
如果我没记错的话,gps可能需要一段时间才能启动。你可以试试
String mBestProvider = loc.getBestProvider(new Criteria(), true);
loc.requestLocationUpdates(mBestProvider, 0, 0, this);
答案 1 :(得分:1)
如果您使用Google Maps v2 for Android和Location API,那么您将使用旧的获取位置更新的方式与您必须实施的新方式混合使用。
我的建议是遵循新位置API的官方教程:https://developer.android.com/training/location/receive-location-updates.html
如您所见,您不必使用LocationManager
实例来请求更新,而是LocationRequest
。
答案 2 :(得分:0)
您的代码没有任何问题。 GPS在这里很棘手。如果您将其设置为找到您的位置,则必须确保您的GPS已开启。这意味着它可能无法在您的模拟器上运行。此外,即使GPS已开启,它仍然很棘手,因为有时候,GPS需要花费大量时间才能找到您的位置。