首页 > 解决方案 > 如果使用双变量实例化 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);

标签: javaandroidgoogle-maps

解决方案


您的 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);
       ......

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

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

推荐阅读