如果使用双变量实例化了LatLng对象,则该对象将不起作用;仅当输入实数时,该对象才会起作用

时间:2019-05-18 11:01:13

标签: java android google-maps

我正在尝试将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);

1 个答案:

答案 0 :(得分:0)

您的var中没有有效的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);
       ......

您可能需要一种将有效值传递给var的方法

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