MidMaps is a tiny Google Maps library for Java ME applications.
With MidMaps you can integrate Google Maps into your J2ME applications in few, easy steps!
How to use it?
MidMaps is designed to be simple. You don’t need to deal with threads, connections or other boring stuff like that: all you have to do is to create a map and display it on in your application.
Creating a simple map
To get a map object, you have to perform two steps:
- First, instantiate a new GoogleMaps object:
GoogleMaps gMaps = new GoogleMaps();
- Then, create a new GoogleStaticMap instance by using the createMap() method:
GoogleStaticMap map = gMaps.createMap(mapWidth, mapHeight, GoogleStaticMap.FORMAT_PNG);
Once you have created a map, it is necessary to define its handler. The handler has an important role: it gets automatically called each time the map object is updated, or when an error occurs: this way, you can know when you need to repaint the map, or how to notify the user about what’s going on. The handler has to implement theGoogleStaticMapHandler interface, that defines two methods:
public void GoogleStaticMapUpdated(GoogleStaticMap map); public void GoogleStaticMapUpdateError(GoogleStaticMap map, int errorCode, String errorMessage);
The first one is called when a map is updated, so that your MIDlet knows that it must be repainted. The second one gets called when an error occurs (e.g.: when there is a network issue and the map image cannot be downloaded).
Once you’ve created your handler, you have to pass it to the GoogleStaticMap instance with the setHandler() method:
map.setHandler(mapHandler);
Now, how to actually load a map? First, it is necessary to define a location for the map:
- create a GoogleMapsCoordinates instance with the preferred latitude and longitude values
- use the GoogleStaticMap setCenter() method
map.setCenter(new GoogleMapsCoordinates(41.8954656, 12.4823243));
and then, you have to update() the map:
map.update();
After you’ve called update, the map image will be loaded and, when finished (or when an error occurs) the GoogleStaticMapHandler will be called. If the map is correctly loaded, you can actually draw it by using itsdraw(Graphics g, int left, int top, int attach) method.
Complete example
Below you can see a full example on how MidMaps can be used to display a map on a Canvas.
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; import com.jappit.midmaps.googlemaps.GoogleMaps; import com.jappit.midmaps.googlemaps.GoogleMapsCoordinates; import com.jappit.midmaps.googlemaps.GoogleStaticMapHandler; import com.jappit.midmaps.googlemaps.GoogleStaticMap; public class GoogleMapsSimpleCanvas extends Canvas implements GoogleStaticMapHandler { GoogleMaps gMaps = null; GoogleStaticMap map = null; public GoogleMapsSimpleCanvas() { gMaps = new GoogleMaps(); map = gMaps.createMap(getWidth(), getHeight(), GoogleStaticMap.FORMAT_PNG); map.setHandler(this); map.setCenter(new GoogleMapsCoordinates(41.8954656, 12.4823243)); map.setZoom(15); map.update(); } protected void paint(Graphics g) { map.draw(g, 0, 0, Graphics.TOP | Graphics.LEFT); } public void GoogleStaticMapUpdateError(GoogleStaticMap map, int errorCode, String errorMessage) { System.out.println("map error: " + errorCode + ", " + errorMessage); } public void GoogleStaticMapUpdated(GoogleStaticMap map) { repaint(); } }
Markers
You can add markers to a map by using the GoogleMapsMarker class. You create a marker by specifying its location:
GoogleMapsMarker marker = new GoogleMapsMarker(new GoogleMapsCoordinates(41.8954656, 12.4823243));
Optionally, you can customize the marker by setting its color, size and and label (must be a single character):
marker.setColor(GoogleStaticMap.COLOR_GREEN); marker.setColor(GoogleMapsMarker.SIZE_TINY); marker.setLabel('P');
Then, you add the marker to your GoogleStaticMap instance with the addMarker() method:
map.addMarker(marker);
Here’s an example of 2 different markers added to the previous map.
GoogleMapsMarker redMarker = new GoogleMapsMarker(new GoogleMapsCoordinates(41.8954656, 12.4823243)); redMarker.setColor(GoogleStaticMap.COLOR_RED); redMarker.setSize(GoogleMapsMarker.SIZE_MID); GoogleMapsMarker blueMarker = new GoogleMapsMarker(new GoogleMapsCoordinates(41.8964656, 12.4843243)); blueMarker.setColor(GoogleStaticMap.COLOR_BLUE); blueMarker.setSize(GoogleMapsMarker.SIZE_SMALL); blueMarker.setLabel('R'); map.addMarker(redMarker); map.addMarker(blueMarker);
Paths
Paths can be easily defined by using the GoogleMapsPath class. To create a new Path, just istantiate a new GoogleMapsPath object:
GoogleMapsPath path = new GoogleMapsPath();
And then add the path points with the addPoint() method:
path.addPoint(new GoogleMapsCoordinates(41.8954656, 12.4823243)); path.addPoint(new GoogleMapsCoordinates(41.8934656, 12.4833243));
Path weight, color and fill color can be customized with the following methods:
bluePath.setColor(GoogleStaticMap.COLOR_BLUE); bluePath.setWeight(5); bluePath.setFillColor(GoogleStaticMap.COLOR_GREEN);
Below you can see an example of 2 different paths added to the map defined above.
GoogleMapsPath path = new GoogleMapsPath(); path.addPoint(new GoogleMapsCoordinates(41.8954656, 12.4823243)); path.addPoint(new GoogleMapsCoordinates(41.8934656, 12.4833243)); path.addPoint(new GoogleMapsCoordinates(41.8944656, 12.4843243)); path.setColor(GoogleStaticMap.COLOR_RED); path.setWeight(10); map.addPath(path); GoogleMapsPath bluePath = new GoogleMapsPath(); bluePath.addPoint(new GoogleMapsCoordinates(41.8954656, 12.4823243)); bluePath.addPoint(new GoogleMapsCoordinates(41.8964656, 12.4813243)); bluePath.addPoint(new GoogleMapsCoordinates(41.8934656, 12.4803243)); bluePath.setColor(GoogleStaticMap.COLOR_BLUE); bluePath.setFillColor(GoogleStaticMap.COLOR_GREEN); bluePath.setWeight(5); map.addPath(bluePath);
Geocoding
Geocoding requires a Google Maps API key, so you have to get your own key to use this feature.
To start, you have to instantiate a Google Maps object by using your own API key:
GoogleMaps gMaps = new GoogleMaps("<your_api_key>");
Then, you have to get a GoogleMapsGeocoder instance by using the createGeocoder() method:
GoogleMapsGeocoder geocoder = gMaps.createGeocoder();
Then, as done for the GoogleStaticMap objects, also the GoogleMapsGeocoder objects require an handler to be defined. The GoogleMapsGeocoderHandler interface defines 2 methods:
public void GoogleMapsGeocodeSuccess(String address, GoogleMapsCoordinates coordinates, int accuracy); public void GoogleMapsGeocodeError(String address, int errorCode, String errorDescription);
These 2 methods are called when a geocoding request successes or fails, respectively. Once you have implemented your GoogleMapsGeocoderHandler, you have to pass it to the GoogleMapsGeocoder instance created above:
geocoder.setHandler(geocoderHandler);
Done this, all is ready to geocode an address: to do this, just call the geocodeAddress() method:
geocoder.geocodeAddress("Rome, Italy");
When the geocoding ends, your handler will be notified via the GoogleMapsGeocodeSuccess() method. A possible implementation of such method is visible below:
public void GoogleMapsGeocodeSuccess(String address, GoogleMapsCoordinates coordinates, int accuracy) { map.setCenter(coordinates); map.addMarker(new GoogleMapsMarker(coordinates)); map.setZoom(GoogleMaps.getZoomForAccuracy(accuracy)); map.update(); }
Since the geocoding operation returns an “accuracy” value, you can use this value to get a default zoom value for your map. To do this, you can use the GoogleMaps.getZoomForAccuracy() method, as shown above.
Below you can see the map generated by the previous code:
相关推荐
Java 实现 Google Maps API 是一个将谷歌地图服务集成到Java应用程序中的过程,它允许开发者利用谷歌地图的强大功能,如定位、路线规划、地理编码、覆盖图层等。本篇文章将深入探讨如何通过Java来调用Google Maps ...
谷歌地图GoogleMaps3.310 谷歌地图GoogleMaps3.310 谷歌地图GoogleMaps3.310 谷歌地图GoogleMaps3.310
Google Maps API是Google提供的一项强大的服务,允许开发者在自己的应用程序中集成地图功能,进行地理位置相关的开发。这个"Google Maps API编程资源大全"包含了丰富的资料,帮助开发者深入理解和使用这一技术。 ...
Google APIs Client Library for Java 是一套强大的工具,它允许开发者轻松地与Google的各种API进行交互,包括Google Drive、Google Calendar、Google Maps等。这个库为Java开发者提供了简单易用的接口,使他们能够...
Google 地图服务的 Java 客户端 提示如果您正在寻找以下 API 的 Java 客户端库,请参阅Java 版云客户端库(版本) 中的 Google Maps Platform API。地址验证 API数据集 API地点 API(新)路线 API新的 API 不会添加到...
【Google Maps Library 0.1.6】是一个专为Delphi开发者设计的库,用于集成谷歌地图服务到他们的应用程序中。这个压缩包包含了不同版本的Delphi开发环境下的项目文件和组项目文件,使得在各种Delphi版本下都能顺利...
具有转弯导航功能的免费,快速,详细和完全离线的地图-受...OSM是Google Maps,Mapquest和Waze的开源替代方案。 快速可靠 离线搜索,GPS导航以及优化的地图可有效节省存储空间。 书签 保存您喜欢的位置并与朋友分享。
GMLib (Google Maps Library) are a components for Delphi/C++ Builder that encapsulate the Google Maps API and thus be able manage Google maps showing easily the result in a browser (TWebBrowser). ...
标题中的“flex做的googlemaps”指的是使用Adobe Flex技术来开发Google Maps的应用程序。Flex是一种基于ActionScript 3.0的开放源代码框架,用于构建富互联网应用程序(RIA),它可以与Flash Player或Adobe AIR运行...
Google Maps API V3 是谷歌为开发者提供的一个强大工具,用于在网站或应用程序中集成交互式地图功能。这个API允许开发人员自定义地图显示的内容、样式以及交互方式,从而构建出各种基于地理位置的应用。在Google ...
3. **Java Web 应用集成**:在 Java Web 应用中集成 Google Maps 通常涉及在 JSP 或 Servlet 中嵌入 JavaScript 代码,利用 Google Maps API 创建地图,并使用 Ajax 来实现服务器端的通信。这使得用户可以在不刷新...
《谷歌地图Google Maps 4.5.3 for Blackberry:移动导航的革命》 谷歌地图Google Maps是一款全球知名的在线地图服务,它为用户提供了一个全面、准确且实时更新的地图平台。在移动设备上,尤其是Blackberry操作系统...
Google Maps 4.5是针对多种平台发布的,包括Android、iOS以及早期的Java ME平台,后者使得它能够兼容非智能系统的黑莓手机,如8220。这一版本的Google Maps优化了对低内存设备的支持,确保在黑莓8220上流畅运行。 2...
Google Maps Downloader 6.63 破解版 这个版本是目前最新的,压缩包里面有gmd.exe和gmd2.exe两个文件,gmd.exe是官方版本的,gmd2.exe是破解后的版本。 需要注意的是,gmd2.exe在运行后标题栏上依然会有(Trial)字样...
在IT行业中,Google Maps是一个非常重要的工具,尤其在地理信息系统(GIS)和定位服务领域。本文将深入探讨Google Maps的功能、使用方法以及如何在地图上进行标注。 首先,Google Maps是一款由Google公司提供的在线...
适用于Google Maps Services的Java客户端描述使用Java? 是否要对某项进行? 寻找? 也许? 该库将带到您的服务器端Java应用程序。 Google Maps Services的Java客户端是用于以下Google Maps API的Java客户端库: 请...
2. **maps_m2.jar**:这是Java应用程序的主代码文件,包含了谷歌地图V2.3.2的全部功能实现,通过jad文件进行调用和执行。 3. **2.jpg & 1.jpg**:可能为应用的截图或者图标,用于展示应用的界面或视觉标识。 4. **...
《图书Google Maps API开发大全》是一本深入探讨Google Maps API技术的专业书籍,旨在帮助开发者全面理解和熟练运用这一强大的地图服务接口。源码文件名为"code",包含了书中各个章节的示例代码,使得读者能够通过...
weather api 查看天气情况,及时了解各地天气
在IT行业中,Google Maps是一个广泛使用的地图服务,它提供了丰富的API和工具,允许开发者进行二次开发,以满足各种定制化需求。"Google Maps 二次开发实例"是一个具体的应用展示,它实现了多项实用功能,包括经纬度...