地图上没有显示位置
Posted
技术标签:
【中文标题】地图上没有显示位置【英文标题】:Location is not not shown on map 【发布时间】:2019-12-24 02:10:45 【问题描述】:我试图通过从 Firebase 实时数据库中检索 Double 值来在地图上显示一个位置。但是当我开始地图活动时,当 Firebase 中存储有 Double 值时,位置位于 0.0,0.0。
我尝试了不同的代码来检索位置,但它们都没有在地图上显示正确的位置。 感谢任何帮助。
MainActivity.Java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
private
RadioGroup route;
public static final String ARG_FROM_MAIN = "arg";
private FirebaseAuth mAuth;
private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
private DatabaseReference mDatabaseReference = mDatabase.getReference().child("Locations");
public String someVariable;
TextView numberbs;
Button getLocationBtn;
private Button btnSignOut;
Button busb1;
Button busb2;
Button busb3;
Button busb4;
Button busb5;
Button busb6;
Button busb7;
Button busb8;
Button busb9;
Button busb10;
Button busb11;
Button busb12;
Button busb13;
Button busb14;
Button busb15;
Button busb16;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberbs = findViewById(R.id.busnotext);
btnSignOut = findViewById(R.id.signoutbtn);
getLocationBtn = findViewById(R.id.getLocationBtn);
numberbs = findViewById(R.id.busnotext);
busb1 = findViewById(R.id.busbtn1);
busb2 = findViewById(R.id.busbtn2);
busb3 = findViewById(R.id.busbtn3);
busb4 = findViewById(R.id.busbtn4);
busb5 = findViewById(R.id.busbtn5);
busb6 = findViewById(R.id.busbtn6);
busb7 = findViewById(R.id.busbtn7);
busb8 = findViewById(R.id.bustbtn8);
busb9 = findViewById(R.id.busbtn9);
busb10 = findViewById(R.id.busbtn10);
busb11 = findViewById(R.id.busbtn11);
busb12 = findViewById(R.id.busbtn12);
busb13 = findViewById(R.id.busbtn13);
busb14 = findViewById(R.id.busbtn14);
busb15 = findViewById(R.id.bustbtn15);
busb16 = findViewById(R.id.busbtn16);
busb1.setOnClickListener(this);
busb2.setOnClickListener(this);
busb3.setOnClickListener(this);
busb4.setOnClickListener(this);
busb5.setOnClickListener(this);
busb6.setOnClickListener(this);
busb7.setOnClickListener(this);
busb8.setOnClickListener(this);
busb9.setOnClickListener(this);
busb10.setOnClickListener(this);
busb11.setOnClickListener(this);
busb12.setOnClickListener(this);
busb13.setOnClickListener(this);
busb14.setOnClickListener(this);
busb15.setOnClickListener(this);
busb16.setOnClickListener(this);
getLocationBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//startService(new Intent(this, MyService.class));
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
intent.putExtra("message", someVariable);
startActivity(intent);
);
/**
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
intent.putExtra("Ref", someVariable);
startActivity(intent);
*/
@Override
public void onClick(View v)
switch (v.getId())
case R.id.busbtn1:
someVariable ="BUS1";
numberbs.setText("Selected: BUS 1");
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS1");
break;
case R.id.busbtn2:
numberbs.setText("Selected: BUS 2");
someVariable ="BUS2";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS2");
break;
case R.id.busbtn3:
numberbs.setText("Selected: BUS 3");
someVariable ="BUS3";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS3");
break;
case R.id.busbtn4:
numberbs.setText("Selected: BUS 4");
someVariable ="BUS4";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS4");
break;
case R.id.busbtn5:
numberbs.setText("Selected: BUS 5");
someVariable ="BUS5";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS5");
break;
case R.id.busbtn6:
numberbs.setText("Selected: BUS 6");
someVariable ="BUS6";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS6");
break;
case R.id.busbtn7:
numberbs.setText("Selected: BUS 7");
someVariable ="BUS7";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS7");
break;
case R.id.bustbtn8:
numberbs.setText("Selected: BUS 8");
someVariable ="BUS8";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS8");
break;
case R.id.busbtn9:
numberbs.setText("Selected: BUS 9");
someVariable ="BUS9";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS9");
break;
case R.id.busbtn10:
numberbs.setText("Selected: BUS 10");
someVariable ="BUS10";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS10");
break;
case R.id.busbtn11:
numberbs.setText("Selected: BUS 11");
someVariable ="BUS11";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS11");
break;
case R.id.busbtn12:
numberbs.setText("Selected: BUS 12");
someVariable ="BUS12";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS12");
break;
case R.id.busbtn13:
numberbs.setText("Selected: BUS 13");
someVariable ="BUS13";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS13");
break;
case R.id.busbtn14:
someVariable ="BUS14";
numberbs.setText("Selected: BUS 14");
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS14");
break;
case R.id.bustbtn15:
numberbs.setText("Selected: BUS 15");
someVariable ="BUS15";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS15");
break;
case R.id.busbtn16:
numberbs.setText("Selected: BUS 16");
someVariable ="BUS16";
mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS16");
break;
MapsActivity.Java
package com.manavchordia.aplparent;
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.0;
Double lati = 0.0;
// String message = "BUS1";
Button Refreshh;
Double latitude = 0.0;
Double longitude = 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();
String str = intent.getStringExtra("message");
vali = str;
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
DatabaseReference myRef = mDatabase.getReference().child("Locations").child(vali);
myRef.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
for (DataSnapshot snapm: dataSnapshot.getChildren())
latitude = snapm.child("latitude").getValue(Double.class);
longitude = snapm.child("longitude").getValue(Double.class);
lati = latitude;
longi = longitude;
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
/**
* 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;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(latitude,longitude);
if (mMap != null)
mMap.addMarker(new MarkerOptions().position(sydney).title(vali));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
Firebase 数据库:https://i.stack.imgur.com/ZiJ7M.png [Firebase 数据库图片][1]
Logcat:
2019-08-18 23:26:33.138 906-916/? E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:25384)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:25504)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfiguration(ActivityManagerService.java:25470)
at com.android.server.wm.WindowManagerService.sendNewConfiguration(WindowManagerService.java:5033)
at com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:2599)
at com.android.server.wm.Session.relayout(Session.java:245)
at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:309)
at com.android.server.wm.Session.onTransact(Session.java:165)
at android.os.Binder.execTransact(Binder.java:752)
2019-08-18 23:26:33.138 906-916/? E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:25384)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:25504)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfiguration(ActivityManagerService.java:25470)
at com.android.server.wm.WindowManagerService.sendNewConfiguration(WindowManagerService.java:5033)
at com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:2599)
at com.android.server.wm.Session.relayout(Session.java:245)
at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:309)
at com.android.server.wm.Session.onTransact(Session.java:165)
at android.os.Binder.execTransact(Binder.java:752)
cfey: UNAUTHENTICATED: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
at cfeu.c(:com.google.android.gms@18385037@18.3.85 (100400-262677519):3)
at sgt.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):79)
at bbrg.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):9)
at bbqz.run(Unknown Source:3)
at bbpv.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):1)
at soj.b(:com.google.android.gms@18385037@18.3.85 (100400-262677519):33)
at soj.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):21)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at suh.run(Unknown Source:7)
at java.lang.Thread.run(Thread.java:764)
2019-08-18 23:26:36.597 23168-23168/? E/MS_LookUpHelper: Lookup exception.
2019-08-18 23:26:36.598 23168-23168/? E/MS_LookUpHelper: Failure while querying in batches.
java.lang.RuntimeException: Err: 17
at bblk.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):76)
at com.google.android.libraries.matchstick.contacts.sync.TachystickSyncChimeraService.onStartCommand(:com.google.android.gms@18385037@18.3.85 (100400-262677519):4)
at ehk.onStartCommand(:com.google.android.gms@18385037@18.3.85 (100400-262677519):1)
at qkj.onStartCommand(:com.google.android.gms@18385037@18.3.85 (100400-262677519):3)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3858)
at android.app.ActivityThread.access$1800(ActivityThread.java:209)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6954)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-08-18 23:26:36.599 23168-24728/? E/MS_ServerQueryProvider: Failed to get server result.
java.util.concurrent.ExecutionException: java.lang.RuntimeException: Err: 17
at bqqe.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):3)
at bqqe.get(:com.google.android.gms@18385037@18.3.85 (100400-262677519):24)
at bqqo.get(Unknown Source:0)
at bbla.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):5)
at bbkd.call(Unknown Source:6)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at soj.b(:com.google.android.gms@18385037@18.3.85 (100400-262677519):33)
at soj.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):18)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at suh.run(Unknown Source:7)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.RuntimeException: Err: 17
at bblk.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):76)
at com.google.android.libraries.matchstick.contacts.sync.TachystickSyncChimeraService.onStartCommand(:com.google.android.gms@18385037@18.3.85 (100400-262677519):4)
at ehk.onStartCommand(:com.google.android.gms@18385037@18.3.85 (100400-262677519):1)
at qkj.onStartCommand(:com.google.android.gms@18385037@18.3.85 (100400-262677519):3)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3858)
at android.app.ActivityThread.access$1800(ActivityThread.java:209)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6954)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
aoqx: 29504: Null server response, http status code 401: 3
at aotx.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):442)
at aotx.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):504)
at aotc.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):14)
at aosz.b(:com.google.android.gms@18385037@18.3.85 (100400-262677519):10)
at aosa.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):92)
at aosa.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):75)
at aaiz.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):19)
at blon.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):2)
at soj.b(:com.google.android.gms@18385037@18.3.85 (100400-262677519):33)
at soj.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):21)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at suh.run(Unknown Source:7)
at java.lang.Thread.run(Thread.java:764)
2019-08-18 23:26:49.569 23155-23318/? E/AsyncOperation: serviceID=51, operation=RegisterSyncOperationCall
OperationException[StatusstatusCode=Null server response, http status code 401: 3, resolution=null]
at aosa.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):100)
at aosa.a(:com.google.android.gms@18385037@18.3.85 (100400-262677519):75)
at aaiz.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):19)
at blon.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):2)
at soj.b(:com.google.android.gms@18385037@18.3.85 (100400-262677519):33)
at soj.run(:com.google.android.gms@18385037@18.3.85 (100400-262677519):21)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at suh.run(Unknown Source:7)
at java.lang.Thread.run(Thread.java:764)
[1]: https://i.stack.imgur.com/ZiJ7M.png
【问题讨论】:
你用的是安卓O吗? 不,我在 OnePlus 6 上使用 Android Pie 7.0后的设备有一些变化,我已经添加了一个答案希望它会有所帮助 【参考方案1】:从 android O 你应该像这样启动你的服务:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
context.startForegroundService(intent);
else
context.startService(intent);
在你的情况下FreshchatService
。
【讨论】:
不,我不使用 FreshchatService 从我从你的日志中看到的,你正在使用这个服务,还是你从 Freshchat SDK 获得的?然后为他们填写错误,他们的 SDK 包含此问题。 我已经更新了我的 Logcat 请看一下。谢谢【参考方案2】:对于 8.0 之前的设备,您必须使用 startService(),但对于 7.0 之后的设备,您必须使用 startForgroundService()。这里有一个 启动服务的代码示例。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
context.startForegroundService(new Intent(context, ServedService.class));
else
context.startService(new Intent(context, ServedService.class));
【讨论】:
但是我要开始活动了【参考方案3】:当从 firebase 获取位置时,您并没有刷新地图 .. 您只是将位置存储在全局变量中
您的代码获取 latitude 和 longitude 也是错误的。下面的代码应该可以工作
for (DataSnapshot snapm: dataSnapshot.getChildren())
String key = snapm.getKey();
if(key.equalsIgnoreCase("latitude"))
lati=snapm.getValue(Double.class);
if(key.equalsIgnoreCase("longitude"))
longi=snapm.getValue(Double.class);
// add this code here
if (mMap != null)
LatLng sydney = new LatLng(lati,longi);
mMap.addMarker(new MarkerOptions().position(sydney).title(vali));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
【讨论】:
【参考方案4】:无需使用getChildren()
方法遍历DataSnapshot
对象,只需使用以下几行代码即可:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference busRef = rootRef.child("Locations").child("BUS11");
ValueEventListener valueEventListener = new ValueEventListener()
@Override
public void onDataChange(DataSnapshot ds)
double lat = ds.child("latitude").getValue(Double.class);
double lng = ds.child("longitude").getValue(Double.class);
LatLng latLng = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng).title("BUS11"));
Log.d(TAG, lat + ", " + lng);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
;
busRef.addListenerForSingleValueEvent(valueEventListener);
使用此代码的结果将是在您的地图上添加一个标记,并在 logcat 中打印以下行:
13.0641221, 80.2500812
【讨论】:
嗨 Ghost103!您是否尝试过我上面的解决方案?如果您认为我的回答对您有所帮助,请考虑通过单击左侧投票箭头下方的复选标记(✔️)来接受它。应将颜色更改为绿色。我会很感激的。谢谢!以上是关于地图上没有显示位置的主要内容,如果未能解决你的问题,请参考以下文章
如何在swift的地图视图上显示位置?我相信我的代码是最新的,但模拟器没有显示位置或蓝点?