我正在尝试获取坐标,但没有成功。结果是0。但是当我写道:
googleMap.setMyLocationEnabled(true);
我可以看到蓝点。我试图用这段代码获取这些值:
lat = googleMap.getMyLocation().getLatitude();
lng = googleMap.getMyLocation().getLongitude();
之后,lat
和lng
为0。
我的整个代码:
public class GPS_maps extends Activity {
// Google Map
private GoogleMap googleMap;
static Location currentLocation;
static double lat , lng;
TextView coordinates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
coordinates = (TextView) findViewById(R.id.txt_coordinates);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// googleMap.setMyLocationEnabled(true);
lat = googleMap.getMyLocation().getLatitude();
lng = googleMap.getMyLocation().getLongitude();
coordinates.setText(String.valueOf(lat) +" , " +String.valueOf(lng));
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
googleMap.animateCamera(yourLocation);
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}}
}
我没有收到任何错误。我的错误在哪里?
答案 0 :(得分:0)
尝试以下方法:
从lat,lan的声明中删除static修饰符。 确保您使用正确的语法发送setCoordiante。 要确保lat和lan是正确的,请在得到它们后立即打印出来。