我有一个从服务器获取通知的应用程序,当应用程序在后台并点击通知时, map Activity 打开并且应该在 map 中显示从通知获取纬度和经度的标记。我的代码中的每个日志都可以工作,直到代码到达也添加标记但它不显示任何内容。这是我的代码:
@Override
public void onConnected(Bundle bundle) {
Log.e("onConnect", "here");
.
.
.
ShowUserRequest();
}
显示用户请求():
public void ShowUserRequest(){
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
//bundle must contain all info sent in "data" field of the notification
Log.e("bundle", bundle.getString("msg")+"");
try {
if(bundle.getString("msg") != null){
JSONArray jsonArray = new JSONArray(bundle.getString("msg"));
JSONObject jsonObject = jsonArray.getJSONObject(0);
name.setText(jsonObject.getString("customer_name"));
phone.setText(jsonObject.getString("phone"));
description.setText(jsonObject.getString("description"));
address.setText(jsonObject.getString("address"));
user_latitude = jsonObject.getDouble("latitude");
user_longitude = jsonObject.getDouble("longitude");
Log.e("lat_lang",user_latitude + user_longitude+"");
ShowUserLocation(user_latitude,user_longitude,jsonObject.getString("customer_name"),
jsonObject.getString("phone"),jsonObject.getString("description"),jsonObject.getString("address"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
显示用户位置() :
public void ShowUserLocation(double latitude , double longitude , final String name_st ,final String phone_st , final String description_st , final String address_st){
Log.e("dataaa",latitude + " " + longitude + " " +name_st + " " + phone_st + " " + description_st + " " + address_st + "");
if(name_st != null && !name_st.equals("") && !name_st.equals("null") ){
name.setText(name_st);
}
if(phone_st != null && !phone_st.equals("") && !phone_st.equals("null")) {
phone.setText(phone_st);
}
if(description_st != null && !description_st.equals("") && !description_st.equals("null") ) {
description.setText(description_st);
}
if(address_st != null && !address_st.equals("") && !address_st.equals("null") ) {
address.setText(address_st);
}
LatLng UserlatLng = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(UserlatLng);
markerOptions.title(name_st);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18.0f));
userLocationMarker = googleMap.addMarker(markerOptions);
}
最佳答案
Try to implement the OnMapReadyCallback in your activity
and override the onMapReady method of Google Map to add marker
@Override
public void onMapReady(GoogleMap map)
{
googleMap = map;
LatLng UserlatLng = new LatLng(latitude, longitude);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(UserlatLng, 10));
map.addMarker(new MarkerOptions().position(UserlatLng));
map.getUiSettings().setMapToolbarEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLng(UserlatLng));
map.animateCamera(CameraUpdateFactory.zoomTo(15));
map.getUiSettings().setZoomControlsEnabled(true);
}
关于android - 后台收到通知时在谷歌地图中添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45975911/