即使在代码中手动设置位置后,标记也不会显示在地图上

Posted

技术标签:

【中文标题】即使在代码中手动设置位置后,标记也不会显示在地图上【英文标题】:Marker not showing up on map even after setting location manually in code 【发布时间】:2019-12-22 03:36:22 【问题描述】:

我正在尝试从 Firebase 数据库中检索双精度值(经度和纬度)并将其显示在地图上。问题是标记没有显示在地图上,我认为这是一个数据库错误,但即使我在代码本身中添加纬度和经度,标记也没有显示(如下所示)。请帮助我,谢谢!

我尝试了从数据库中检索数据的不同方法以及添加标记的不同方法。

MapsActivity.java

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback

    private GoogleMap mMap;
    private FirebaseAuth mAuth;
    //String value_latitude = "0";
   // String value_longitue = "0";
    double longi = 0;
    double lati = 0;
   // String message = "BUS1";
    Button Refreshh;
    Double latitude = 0.0;
    Double longitdue = 0.0;
    String vali = "BUS1";
    String message = "BUS1";
    Button Backkk;

    private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference mDatabaseReference = mDatabase.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        //Intent intent = getIntent();
        message = getIntent().getExtras().getString("arg");
        vali = message;

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        //vali = message;

        //Log.d("vali", vali);


    



    /**
     *
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) 
        mMap = googleMap;
        mMap.clear();


        mDatabaseReference.child("Locations").child(vali).addChildEventListener(new ChildEventListener() 
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String prevChildKey) 
                LatLng newLocation = new LatLng(
                        dataSnapshot.child("latitude").getValue(Long.class),
                        dataSnapshot.child("longitude").getValue(Long.class)
                );
                  LatLng sydney = new LatLng(13.0640934, 80.2501135);
                mMap.addMarker(new MarkerOptions().position(sydney)
                        .title("Marker in Sydney"));
              mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, String prevChildKey) 

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) 

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, String prevChildKey) 

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) 
        );
    

我的 Logcat:

2019-08-15 23:03:09.134 4869-5490/? E/...lback.CallbackFuture: Timed out waiting for result!
2019-08-15 23:03:09.135 4869-5490/? E/AmazonKindle.com.amazon.kindle.services.authentication.MAPTokenCache: [WebRequestManager-webserviceRequest2-thread-1]: Retrieving of token: com.amazon.dcp.sso.token.cookie.xmainAndXabcCookies failed
    java.util.concurrent.TimeoutException: Timed out waiting for result!
        at com.amazon.identity.auth.device.callback.CallbackFuture.get(CallbackFuture.java:170)
        at com.amazon.identity.auth.device.callback.CallbackFuture.get(CallbackFuture.java:26)
        at com.amazon.kindle.services.authentication.MAPTokenCache.retrieveToken(MAPTokenCache.java:378)
        at com.amazon.kindle.services.authentication.MAPTokenCache.fetchTokenManagementToken(MAPTokenCache.java:338)
        at com.amazon.kindle.services.authentication.MAPTokenCache.fetchValue(MAPTokenCache.java:263)
        at com.amazon.kindle.services.authentication.MAPTokenCache.getValue(MAPTokenCache.java:156)
        at com.amazon.kindle.services.authentication.AccountHoldersKt.getToken(AccountHolders.kt:119)
        at com.amazon.kindle.services.authentication.KindleAuthenticationManager.getToken(KindleAuthenticationManager.kt:185)
        at com.amazon.kindle.krx.application.DeviceInformation.getXmainXacbCookieForDevicePfm(DeviceInformation.java:137)
        at com.amazon.kcp.library.customerbenefits.CustomerBenefitsWebRequest.getHeaders(CustomerBenefitsWebRequest.java:102)
        at com.amazon.kindle.webservices.BaseWebRequestExecutor.connect(BaseWebRequestExecutor.java:536)
        at com.amazon.kindle.webservices.BaseWebRequestExecutor.openConnection(BaseWebRequestExecutor.java:200)
        at com.amazon.kindle.webservices.BaseWebRequestExecutor.execute(BaseWebRequestExecutor.java:121)
        at com.amazon.kindle.webservices.WebRequestManager$WebRequestTask.executeRequest(WebRequestManager.java:125)
        at com.amazon.kindle.webservices.WebRequestManager$WebRequestTask.run(WebRequestManager.java:98)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

【问题讨论】:

请将您的数据库结构添加为 JSON 文件或至少作为屏幕截图。 【参考方案1】:

您在 ChildEventListener 中添加标记,onChildAdded() 仅在每个现有子项以及新子项添加到您正在收听的引用中时调用。 因此,如果您的引用没有任何子项,则不会调用它。

检查您的 FirebaseReference,您可能访问了错误的孩子。 或者您可能使用了错误的侦听器,如果您将代码移出侦听器,您会看到它确实显示了标记。 如果要获取子引用的值,则必须添加 ValueEventListener:

ref.addValueEventListener(new ValueEventListener() 
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) 
    Post post = dataSnapshot.getValue(Post.class);
    System.out.println(post);
  

  @Override
  public void onCancelled(DatabaseError databaseError) 
    System.out.println("The read failed: " + databaseError.getCode());
  
);

【讨论】:

Firebase 数据库中有一个参考,但由于某种原因该标记未显示在地图上。即使我直接在代码中添加坐标

以上是关于即使在代码中手动设置位置后,标记也不会显示在地图上的主要内容,如果未能解决你的问题,请参考以下文章

应用程序启动地图活动后,如何在 android 的地图中显示我的当前位置

如何保存用户位置并使用图钉(或其他标记)将其显示在地图上?

如何引入百度地图?

我在网页上想调用百度地图,如何实现百度地图的标记功能,在html文件里写js代码

在 AngularJS 应用程序中单击 Google 地图中的标记不会更新显示的数据

有没有办法在 AGM 地图中设置边界和缩放级别?