2013-11-26 73 views
3

使用谷歌地图API v2,我知道myMap.setMyLocationEnabled(true);将让我显示谷歌地图上的用户位置,但我也希望此位置的坐标显示在谷歌地图下方。我不想使用如何获取用户当前位置坐标使用谷歌地图API而不是位置管理器

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

因为这看起来像一个矫枉过正。我们已经有了用户的位置(在地图上指向的针),现在我只需要它的坐标值。 我如何得到?我无法找到某些属性,如myLocation.getCurrentCoordinates()

+0

过度杀伤力有什么问题?这是确保某些东西可行的一种手段。 –

+0

@AleksanderLidtke有时候,矫枉过正会占用更多的资源/时间,这可能会对移动应用程序造成轻微的伤害,因为这些已经很少了。 –

回答

1

我结束了使用LocationClient让我的当前位置,并且依赖于getLastLocation()

private LocationClient locationClient; 
private Location myLocation = null; 
locationClient = new LocationClient(this, this, this); 
locationClient.connect(); 

    @Override 
public void onConnected(Bundle arg0) { 
    myLocation = locationClient.getLastLocation(); 
} 

这可能并不总是正确的,但我只需要它曾经并不想使用locationChangedListeners。

3

尝试使用:

myMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() { 

      @Override 
      public void onMyLocationChange(Location location) { 
       // TODO Auto-generated method stub 

      } 
     }); 

您将收到它改变了位置每次。如果您只想要它,您可以使用

Location location = myMap.getMyLocation() 
+1

'Location location = myMap.getMyLocation()'已被弃用,不应该被使用,至于OnMyLocationChangeListener,我可能会使用那个 –

+1

舒尔,但ActivityGroup已被弃用,因为Android 2.3 ...仍然存在 –

+0

我用而不是locationClient,myLocation = locationClient.getLastLocation(); –

0

试试这个太:

GPSTracker tracker = new GPSTracker(this); 
    if (tracker.canGetLocation() == false) { 
     tracker.showSettingsAlert(); 
    } else { 
     latitude = tracker.getLatitude(); 
     longitude = tracker.getLongitude(); 
    } 
0
//this might help u out 

    enter code here 
ar initialLocation; 
var siberia = new google.maps.LatLng(60, 105); 
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 
var browserSupportFlag = new Boolean(); 

function initialize() { 
    var myOptions = { 
    zoom: 6, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions); 

    // Try W3C Geolocation (Preferred) 
    if(navigator.geolocation) { 
    browserSupportFlag = true; 
    navigator.geolocation.getCurrentPosition(function(position) { 
     initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
     map.setCenter(initialLocation); 
    }, function() { 
     handleNoGeolocation(browserSupportFlag); 
    }); 
    } 
    // Browser doesn't support Geolocation 
    else { 
    browserSupportFlag = false; 
    handleNoGeolocation(browserSupportFlag); 
    } 

    function handleNoGeolocation(errorFlag) { 
    if (errorFlag == true) { 
     alert("Geolocation service failed."); 
     initialLocation = newyork; 
    } else { 
     alert("Your browser doesn't support geolocation. We've placed you in Siberia."); 
     initialLocation = siberia; 
    } 
    map.setCenter(initialLocation); 
    } 
} 
+0

@ user376060对不起,这不适用于Android –

0

为时已晚,但对于新的位置:

GoogleMap gMap; 

double lat = gMap.getCameraPosition().target.latitude; 

double lng = gMap.getCameraPosition().target.longitude); 

float zoom = gMap.getCameraPosition().zoom); 

你得到绝对的相机位置。

相关问题