java - 如果使用双变量实例化 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);
......
可能是您需要一种将有效值传递给您的变量的方法
public void onMapReady(GoogleMap googleMap, latitude, longitude) {
map = googleMap;
LatLng myPostition = new LatLng(latitude, longitude);
......
推荐阅读
- sql - SELECT/HAVING 的 SQL 查询异常的执行顺序
- php - 添加一个表单域添加一个数学问题来阻止机器人?:(
- python - 在 Python 中,是否有一个内置函数来获取两个给定值之间的值(在列表中)?
- verilog - 多位门
- node.js - 为什么我的 XMLHttpRequest POST 并不总是发送到服务器?
- python - 如何在opencv python中放置停留时间
- javascript - React Usestate 将状态设置为 false,我正在使用其中包含对象的数组
- php - 需要 curl 等待响应直到下一次执行
- c# - 在 dotnet core 3.1 中使用 IAuthorizationPolicyProvider 的自定义实现进行身份验证
- node.js - MongoDB - 我怎样才能获得最高的 Int