如何在谷歌地图android中获取当前位置 [英] how to get current location in google map android
本文介绍了如何在谷歌地图android中获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
if(googleMap == null){
googleMap =((MapFragment)getFragmentManager()。findFragmentById (
R.id.map))。getMap();
//检查映射是否成功创建
if(googleMap == null){
Toast.makeText(getApplicationContext(),
Sorry!unable创建地图,Toast.LENGTH_SHORT)
.show();
}
}
googleMap.setMyLocationEnabled(true);
位置myLocation = googleMap.getMyLocation(); //空指针异常.........
LatLng myLatLng = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
CameraPosition myPosition = new CameraPosition.Builder()
.target(myLatLng).zoom(17).bearing(90).tilt(30).build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(myPosition));
解决方案
请检查示例代码 Google Maps Android API v2 。使用它可以解决你的问题。
private void setUpMapIfNeeded(){
//执行空检查以确认我们还没有实例化地图。
if(mMap == null){
//尝试从SupportMapFragment获取映射。
mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
//检查我们是否成功获取地图。
if(mMap!= null){
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener(){
@Override
public void onMyLocationChange(Location arg0){
// TODO自动生成的方法存根
mMap.addMarker(new MarkerOptions()。position(new LatLng(arg0.getLatitude(),arg0.getLongitude ()))。title(It's Me!));
}
});
$ b
函数在 onCreate
函数中。
Actually my problem is I am not getting current location latitude and longitude I tried so many ways.I know that this question already asked in SO I tried that answers also still I didn't get answer.Please help me Code:
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
googleMap.setMyLocationEnabled(true);
Location myLocation = googleMap.getMyLocation(); //Nullpointer exception.........
LatLng myLatLng = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
CameraPosition myPosition = new CameraPosition.Builder()
.target(myLatLng).zoom(17).bearing(90).tilt(30).build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(myPosition));
解决方案
Please check the sample code for the Google Maps Android API v2. Using this will solve your problem.
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
}
});
}
}
}
Call this function in onCreate
function.
这篇关于如何在谷歌地图android中获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文