2015-02-23 81 views
0

我創建了一個顯示谷歌地圖和標記的基本應用程序。標記出現在地圖上,無論我點擊哪處,都會清除以前的位置。現在,我試圖在標記上顯示位置名稱/地址/區域名稱,無論我點擊Google地圖。但不幸的是我的應用程序正在關閉。我暫時用一個吐司來確認顯示。我使用的監聽器是這樣的:在Google地圖上的標記上顯示當前位置/地址/地區名稱

Temporary.java文件

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() { 

      @Override 
      public boolean onMarkerClick(Marker marker) { 
       List<Address> address = null; 
       Geocoder geocode; 
       // TODO Auto-generated method stub 
       geocode = new Geocoder(Temporary.this,Locale.getDefault()); 
       try { 
        address=geocode.getFromLocation(currentGPS.latitude, currentGPS.longitude, 1); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       String address1=address.get(0).getCountryName(); 
       Toast.makeText(Temporary.this,address1, Toast.LENGTH_LONG).show(); 
       return false; 
      } 
     }); 

我想多了一個監聽器,如下所示:

 googleMap.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng point) { 

      List<Address> address = null; 
      Geocoder geocode; 
      // TODO Auto-generated method stub 
      geocode = new Geocoder(Temporary.this,Locale.getDefault()); 
      try { 
       address=geocode.getFromLocation(point.latitude, point.longitude, 1); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.position(point); 
      markerOptions.title("Area Name :"+address.get(0).getLocality()); 
      googleMap.clear(); 
      googleMap.addMarker(markerOptions); 

     } 
    }); 

不過它不工作。我錯在哪裏?

回答

0

你可以設置在你的地圖信息窗口一和定製它的當前選擇。

 map.setInfoWindowAdapter(new InfoWindowAdapter() { 

        @Override 
        public View getInfoWindow(Marker arg0) { 
         return null; 
        } 

        @Override 
        public View getInfoContents(Marker arg0) { 
         currentInfo = getLayoutInflater().inflate(R.layout.info_layout, null); 
         //Do what you want with the view 
         return currentInfo; 
        } 
     } 

記住的標記有他自己的位置,標題,顏色等

相關問題