java - 如果使用双变量实例化 LatLng 对象,则该对象将不起作用,只有在输入实数时才起作用

标签 java android google-maps

我正在尝试将谷歌地图 API 用于学校项目。当我使用两个硬编码数字创建 LatLng 对象时, map 效果很好。当我尝试使用两个双变量时它不起作用?请帮忙,我快疯了!

请相信我,变量纬度和经度被分配给完美的 double 值,当打印为字符串时恰好是 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);

最佳答案

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

关于java - 如果使用双变量实例化 LatLng 对象,则该对象将不起作用,只有在输入实数时才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198315/

相关文章:

java - Autowired 服务的 ScheduledExecutorService 问题

java - 使用 java -jar 运行代码时如何打印 UTF8

java - 从持久化实体 JPA 中删除列表的对象

Android View InflateException 错误

ios - 在 Google map 上显示和隐藏用户默认位置标记

java - Tomcat 7 中更快的随机生成器

android - 改变TextInputLayout的 float 标签文本的位置

android - 重置/重新加载 fragment 容器

android - 如何将 alpha channel 添加到位图

android - 谷歌地图 API v2 : LatLngBounds from CameraPosition