在谷歌地图上查找位置
Posted
技术标签:
【中文标题】在谷歌地图上查找位置【英文标题】:Finding Location on Google Map 【发布时间】:2013-11-21 09:14:21 【问题描述】:我正在尝试在 Google 地图上查找我的位置。我的代码在这里。我已经在我的手机上运行了我的应用程序,但不幸的是它停止了。关于可能是什么问题的任何想法?谢谢。
public class MainActivity extends Activity implements LocationListener
private LatLng loc ;
private LocationManager locationManager;
private String provider;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null)
onLocationChanged(location);
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions().position(loc).title("Istanbul"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
@Override
public void onLocationChanged(Location location)
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
loc = new LatLng(lat,lng);
@Override
public void onProviderDisabled(String provider)
// TODO Auto-generated method stub
@Override
public void onProviderEnabled(String provider)
// TODO Auto-generated method stub
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
// TODO Auto-generated method stub
@Override
protected void onResume()
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
@Override
protected void onPause()
super.onPause();
locationManager.removeUpdates(this);
【问题讨论】:
【参考方案1】:public class MainActivity extends FragmentActivity implements LocationListener
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS) // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
else // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null)
onLocationChanged(location);
locationManager.requestLocationUpdates(provider, 20000, 0, this);
@Override
public void onLocationChanged(Location location)
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
@Override
public void onProviderDisabled(String provider)
// TODO Auto-generated method stub
@Override
public void onProviderEnabled(String provider)
// TODO Auto-generated method stub
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
// TODO Auto-generated method stub
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
Download Here
【讨论】:
【参考方案2】:// try this
**main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:padding="5dp"
android:orientation="vertical">
<fragment
android:id="@+id/maps"
android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
android:layout_
android:layout_ />
</LinearLayout>
public class MyActivity extends FragmentActivity
public static LocationManager mlocManager;
public static LocationListner mListner;
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = getMapView();
map.setMyLocationEnabled(true);
try
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
mListner = new LocationListner();
runOnUiThread(new Runnable()
@Override
public void run()
try
try
mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
catch (Throwable e)
e.printStackTrace();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
catch (Throwable e)
e.printStackTrace();
try
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
catch (Throwable e)
e.printStackTrace();
);
catch (Throwable e)
e.printStackTrace();
public GoogleMap getMapView()
FragmentManager fm = getSupportFragmentManager();
SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.maps);
// Getting GoogleMap object from the fragment
return f.getExtendedMap();
public void setMapMarker(LatLng loc)
map.addMarker(new MarkerOptions().position(loc).title("Istanbul"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
class LocationListner implements LocationListener
@Override
public void onLocationChanged(Location location)
setMapMarker(new LatLng(location.getLatitude(),location.getLongitude()));
@Override
public void onProviderDisabled(String provider)
@Override
public void onProviderEnabled(String provider)
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
【讨论】:
感谢您的关注。【参考方案3】:在这一行:
map.addMarker(new MarkerOptions().position(loc).title("Istanbul"));
loc 为空
另外,你不应该直接调用 onLocationChanged(),它是一个回调。整个过程是事件驱动的,即当 GPS 或网络为您提供该方法触发的位置时。
【讨论】:
我正在尝试通过在 onLocationChange double lat = (double) (location.getLatitude()); 中实现的这些行为 loc 提供必要的参数双 lng = (double) (location.getLongitude()); loc = new LatLng(lat,lng);没用吗? 我提到的那一行直接在onCreate()中运行,onLocationChanged()直到位置改变才会执行,它是一个回调。因此当 onCreate() 运行时 loc 为空 我知道了,谢谢,但我怎样才能获得当前位置?是否有可以找到当前位置的内置函数? 您必须等待第一次获得定位。之后,您可以使用 getLastKnownLocation()。在那之前,您的位置为空。没有任何方法可以在您的应用启动后立即为您提供位置。以上是关于在谷歌地图上查找位置的主要内容,如果未能解决你的问题,请参考以下文章