2014-01-15 16 views
1

我在BrowserField中顯示谷歌地圖。黑莓手機 - 如何在瀏覽器字段中爲GoogleMap引用位於項目的res/img文件夾中的圖片

這是相關代碼:

private String setUpHtmlString(Coordinates coordinates){ 
     StringBuffer mapString = new StringBuffer(); 
     mapString.append("" + 
       "<!DOCTYPE html> " + 
       "<html> " + 
       " <head> " + 
       "  <meta http-equiv='content-type' content='text/html; charset=UTF-8' /> " + 
       "  <title>Google Maps Multiple Markers</title> " + 
       "  <script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript'></script>" + 
       " </head> " + 
       " <body> " + 
       "  <div id='map' style='width: 500px; height: 600px;'></div> " + 
       "  <script type='text/javascript'> " + 
       "   var locations = [  "); 

       for (int i = 0; i < _placesStringArray.length; i++) { 
        Address address = ((Place)_hashTablePlaces.get(_placesStringArray[i])).getAddress(); 
        mapString.append("['"+address.getDescription()+"', "+address.getLatitude()+", "+address.getLongitude()+", "+i+"]"); 
        if(i<_placesStringArray.length - 1) 
         mapString.append(","); 
       } 

       mapString.append("];"); 

       mapString.append(
       "   var map = new google.maps.Map(document.getElementById('map'), {  " + 
       "    zoom: 15,  " + 
       "    center: new google.maps.LatLng(-25.290646, -57.584080),  " + 
       "    mapTypeId: google.maps.MapTypeId.ROADMAP }); " + 
       "   var infowindow = new google.maps.InfoWindow(); " + 
       "   var marker, i; " + 
       "   for (i = 0; i < locations.length; i++) {  " + 
       "    marker = new google.maps.Marker({  " + 
            "icon:'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png', "+ 
       "     position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map });  " + 
       "    google.maps.event.addListener(marker, 'click', (function(marker, i) {  " + 
       "     return function() { " + 
       "      infowindow.setContent(locations[i][0]);   " + 
       "      infowindow.open(map, marker);  " + 
       "      }  " + 
       "     })(marker, i)); } " + 
       "  </script>" + 
       " </body>" + 
       "</html>"); 
       return mapString.toString(); 
    } 

正如你可以看到,圖標指向一個外部URL,但應如何寫的路徑,我的應用程序的文件夾img 內的圖像文件。

enter image description here

我想引用它在很多方面,像這樣的:

"icon:'local:///assets/images/marker.png' 
"icon:'resources/images/marker.png' 

沒有成功。

在此先感謝。

+1

這取決於最終的目錄結構和您的html文件保存在哪裏。你應該使用「icon:'img/marker.png'」如果'img'和你創建的html文件在同一個目錄下(並且marker.png保存在img目錄中)。 –

+1

感謝Anto,但我沒有html文件,因爲html是從字符串創建的,並傳遞給BrowserField對象的displayContent()方法。 – Lucas

回答

1

我不會建議你對.java文件中的html進行硬編碼,因爲代碼不可讀,也很難找到未來的修改。內聯JavaScript已經是一件壞事,但是在Java代碼中將所有內容都放入一個字符串中是超越耦合的一個步驟。

選項#1:最徹底的方法
相反,你可以有一個資源的HTML文件,並使用 「本地:///」 加載URL,作爲BrowserField演示表明:Display HTML content from a resource in your application

在你可以在同一個目錄下放置javascript和css文件,就像你在普通的靜態網站上做的那樣。所以理論上你也可以在那裏放置圖像資源文件,並從html或js文件中引用它們。 URL不需要完整路徑(例如:而不是local:///assets/images/marker.png,您將擁有local:///marker.png)。

如果您需要將動態內容插入到html中,那麼您始終可以將佔位符插入到模板html資源文件中,然後從java代碼中執行string.replace以用動態字段替換佔位符。

這樣你就可以解決問題的可修改性和分離。

選項#2:骯髒的黑客
我展示了爲什麼你的代碼很乾淨,但在生活中的許多事情仍可能變得更糟。您可以通過將圖像文件編碼爲base64字符串並在標記或JavaScript中爲圖像設置「data://」網址來添加一層不可修改的新圖層。如果圖標非常小,並且事先知道不會頻繁更改,但是請注意base64字符串可能會變得非常大,這對您來說可能是可以接受的。

選項#3(未測試)
這也是一種破解。假設您可以使用Class.getResourceAsStream打開圖像,則可以在java代碼中實例化ProtocolController,然後調用setNavigationRequestHandler來設置攔截您感興趣的請求類型的處理程序,並使用Class.getResourceAsStream傳遞加載的內容。

獎金選項:(未測試)
而這裏到a BB forum post,其中一人表示,您還可以使用開始「鱈魚:///」的URL引用圖像的鏈接。