自定义InfoWindow用于特定标记

时间:2017-03-29 14:14:49

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

我正在使用驾驶室预订应用程序,我使用不同类型的标记(一个用于乘客,多个用于驾驶室)用于乘客标记我不需要自定义InfoWindow,但是用于出租车'(我在哪里显示)关于它们的不同信息,可能会改变)我这样做,因此我已经使用资源布局文件(info_window.xml)定制了InfoWindow,但无论我尝试过什么(甚至使用hideInfoWindow();)我无法隐藏乘客的InfoWindow,每当我尝试将Text设置为InfoWindow TextView应用程序时,如果我正在尝试的是可能的那么我做错了什么?如果不是,是否有其他方法可以在不使用InfoWindows的情况下显示信息,这可能与此类似! enter image description here知道InfoWindow已经转换为Bitmap我也尝试使用Android MapView的简单信息气球注释,就像在这个链接https://github.com/jgilfelt/android-mapviewballoons中一样,但它已被弃用,我该怎么用呢! Ps:我需要一个可点击的按钮。

这是 info_window.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvLatitude"
        android:text="Latitude"
        android:layout_marginRight="15dp"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvLongitude"
        android:text="Longitude"
        android:layout_marginRight="10dp"/>

</LinearLayout>

这些是来自 MapActivity.java

的部分
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private BroadcastReceiver broadcastReceiver;
GoogleMap map;
static Double latt, lngg;
private String[] perms = {"android.permission.ACCESS_COARSE_LOCATION",
        "android.permission.ACCESS_FINE_LOCATION",
        "android.permission.INTERNET"};
HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();
Marker marker;

 @Override
 protected void onResume() {
    super.onResume();
    if(broadcastReceiver == null){
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                getJSON();
                lngg= intent.getDoubleExtra("longitude",-1);
                latt= intent.getDoubleExtra("latitude",-1);

                Marker m =map.addMarker(new MarkerOptions()
                        .position(new LatLng(latt,lngg))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon2))
                        .title("I'm a passenger"));
                     m.hideInfoWindow();
              //  goToLocationZoom(latt, lngg , 15) ;


            }
        };
    }
    registerReceiver(broadcastReceiver, new IntentFilter("location_update"));


}


class BackgroundTask extends AsyncTask<Void,Void,String>{
    String JSON_STRING;
    String json_url ;
    @Override
    protected void onPreExecute() {

        json_url="http://frequentative-hicko.000webhostapp.com/FetchLocAndId.php";
    }

    @Override
    protected String doInBackground(Void... voids) {

        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader= new BufferedReader( new InputStreamReader(inputStream));
            StringBuilder stringBuilder= new StringBuilder();
            while( (JSON_STRING = bufferedReader.readLine()) != null ){

                stringBuilder.append(JSON_STRING+"\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();




        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String response) {
        TextView textView =(TextView) findViewById(R.id.textView2);
      //  textView.setText(response); //JSON Format

        // System.out.println("json_string="+json_string);
        if(response== null){

            Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG);

        }
        else {

            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("response");
                int i=0 ;
                int id,e;
                Double lt,lg;
                while (i<jsonArray.length()) {
                    JSONObject jo = jsonArray.getJSONObject(i);
                    e=  jo.getInt("etat");

                    id= jo.getInt("driver_id");

                    lt=jo.getDouble("latitude");

                    lg= jo.getDouble("longitude");

                     if(e==0){
                         marker = markerMap.get(id);
                         if(marker != null){
                             marker.remove();
                             markerMap.remove(id);
                         }else
                         {
                             markerMap.remove(id);
                         }
                     }else{
 map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                             // Use default InfoWindow frame
                             @Override
                             public View getInfoWindow(Marker arg0) {


                       return null;
                             }

                             // Defines the contents of the InfoWindow
                             @Override
                             public View getInfoContents(Marker arg0) {

                                 // Getting view from the layout file info_window_layout
                                 View v = getLayoutInflater().inflate(R.layout.info_window, null);

                                 // Getting the position from the marker
                                 TextView tvLatitude= (TextView) findViewById(R.id.tvLatitude);
                                tvLatitude.setText("Latitude ");
                                 // Returning the view containing InfoWindow contents
                                 return v;

                             }
                         });
                         if (markerMap.containsKey(id)) {

                    // Update the location.
                    marker = markerMap.get(id);
                    marker.remove();
                    markerMap.remove(id); //added
                    MarkerOptions markerOpt = new MarkerOptions()
                            .title("route_direct").position(new LatLng(lt,lg)).visible(true);
                    marker = map.addMarker(markerOpt);
                    markerMap.put(id, marker);

                    } else {

                        MarkerOptions markerOpt = new MarkerOptions()
                                .title("route_direct").position(new     LatLng(lt, lg)).visible(true);

                        marker = map.addMarker(markerOpt);
                        markerMap.put(id, marker);

                    }}

                    i++;}


                    }catch (JSONException e) {
            e.printStackTrace();
            AlertDialog.Builder nbuilder = new AlertDialog.Builder(MapActivity.this);
            nbuilder.setMessage("Erreur")
                    .setNegativeButton("Retry", null)
                    .create()
                    .show();
            }

        }

   }
   }
   }

这是我得到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: androidloginandregistration.inducesmile.com.ataxiapp, PID: 7484
              java.lang.NullPointerException
                  at androidloginandregistration.inducesmile.com.ataxiapp.MapActivity$BackgroundTask$1.getInfoWindow(MapActivity.java:331)
                  at com.google.android.gms.maps.GoogleMap$7.zzh(Unknown Source)
                  at com.google.android.gms.maps.internal.zzd$zza.onTransact(Unknown Source)
                  at android.os.Binder.transact(Binder.java:361)
                  at com.google.android.gms.maps.internal.p.a(:com.google.android.gms.DynamiteModulesB:94)
                  at com.google.maps.api.android.lib6.impl.by.a(:com.google.android.gms.DynamiteModulesB:89)
                  at com.google.maps.api.android.lib6.impl.by.a(:com.google.android.gms.DynamiteModulesB:124)
                  at com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:188)
                  at com.google.maps.api.android.lib6.gmm6.api.g.c(:com.google.android.gms.DynamiteModulesB:200)
                  at com.google.maps.api.android.lib6.impl.db.g(:com.google.android.gms.DynamiteModulesB:23225)
                  at com.google.maps.api.android.lib6.impl.dd.b(:com.google.android.gms.DynamiteModulesB:304)
                  at com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:242)
                  at com.google.maps.api.android.lib6.gmm6.vector.m.a(:com.google.android.gms.DynamiteModulesB:4070)
                  at com.google.maps.api.android.lib6.gmm6.vector.af.c(:com.google.android.gms.DynamiteModulesB:611)
                  at com.google.maps.api.android.lib6.gmm6.vector.df.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:236)
                  at com.google.maps.api.android.lib6.impl.gesture.g.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:189)
                  at com.google.maps.api.android.lib6.impl.gesture.i.handleMessage(:com.google.android.gms.DynamiteModulesB:132)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5476)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我在这里给出了步骤

  1. onCreate

    中夸大您的观点
    public class MapActivity extends AppCompatActivity implements    OnMapReadyCallback {
     ... 
     View v;
     ...
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, 0);
        v = getLayoutInflater().inflate(R.layout.info_window, null);
    }
    
  2. getInfoContents 中删除逻辑并添加 getInfoWindow

    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    
                         // Use default InfoWindow frame
                         @Override
                         public View getInfoWindow(Marker arg0) {
    
                             // Getting the position from the marker
                             TextView tvLatitude= (TextView) v.findViewById(R.id.tvLatitude);
                            tvLatitude.setText("Latitude ");
                             // Returning the view containing InfoWindow contents
    
                          return v;
                         }
    
                         // Defines the contents of the InfoWindow
                         @Override
                         public View getInfoContents(Marker arg0) {
                          if (marker != null && marker.isInfoWindowShown()){
                             marker.hideInfoWindow();
                          }
                          return null;
                        }
                     });