【问题标题】:addMarker in google map when notification came in background后台通知时在谷歌地图中添加标记
【发布时间】:2017-08-31 07:41:33
【问题描述】:

我有一个从服务器获取通知的应用程序,当应用程序在后台并点击通知时,地图活动将打开并应在地图中显示从通知中获取纬度和经度的标记。我的代码中的每个日志都可以工作,直到代码到达也添加标记,但它没有显示任何内容。这是我的代码:

 @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);

   }

【问题讨论】:

    标签: android google-maps notifications google-maps-markers intentfilter


    【解决方案1】:
    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);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 2016-03-16
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多