如果latlng对象使用双变量实例化,则仅在填充实际数字时才起作用



我正在尝试将Google Maps API用于学校项目。当我使用两个硬编码数字创建一个latlng对象时,地图效果很好。当我尝试使用两个双变量时,它不起作用?帮助我要疯了!

请相信我,变量的纬度和经度分配给了完美的双打,当打印为字符串时,正好是33.190802,-117.301805

//WORKS PERFECTLY
public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        //Specify our location with latlng class
        LatLng myPostition = new LatLng(33.190802, -117.301805);
        //Add our position to the map
        map.addMarker(new MarkerOptions().position(myPostition).title("Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker)));
        //move camera to our position
        CameraPosition cameraPosition = new CameraPosition.Builder().target(myPostition).zoom(10.0f).build();
        //update the position --> move to the location
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
        //instruct the map to move to this position
        map.moveCamera(cameraUpdate);
//DOSEN'T WORK--But I need latitude and longitude to be set programmatically
public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        //Specify our location with latlng class
        LatLng myPostition = new LatLng(latitude, longitude);
        //Add our position to the map
        map.addMarker(new MarkerOptions().position(myPostition).title("Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker)));
        //move camera to our position
        CameraPosition cameraPosition = new CameraPosition.Builder().target(myPostition).zoom(10.0f).build();
        //update the position --> move to the location
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
        //instruct the map to move to this position
        map.moveCamera(cameraUpdate);

您在vars中没有有效的avlue

 public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    latitude =  33.190802;
    longitude =  -117.301805; 
    //Specify our location with latlng class
     LatLng myPostition = new LatLng(latitude, longitude);
       ......

可能是您需要一种将有效值传递给您的vars

的方法
  public void onMapReady(GoogleMap googleMap, latitude, longitude) {
       map = googleMap;
       LatLng myPostition = new LatLng(latitude, longitude);
       ......

最新更新