简体   繁体   中英

Using google static maps - JFrame or Javascript

I have 2 parts to my question. First part: I followed THIS SO question and am able to display a static google map. But I have a list of lat/long values which I want to display, on the same map, using markers for each of the coordinates. The code using JFrame is below (I added a second set of parameters, just to test). I am able to display only one coordinate and not the second one.

JFrame frame = new JFrame("Google maps");
String destinationFile = "image.jpg";
try {
    String imageUrl = "http://maps.googleapis.com/maps/api/staticmap?zoom=18&size=800x800&markers=45.10232295,-80.14865993&sensor=true";
    URL url = new URL(imageUrl);
    frame.add( new JLabel( new ImageIcon( new ImageIcon(url).getImage().getScaledInstance( 630, 600, java.awt.Image.SCALE_SMOOTH ) ) ) );

    /* Second set to test if same map can display multiple markers */   
    String imageUrl2 = "http://maps.googleapis.com/maps/api/staticmap?zoom=18&size=800x800&markers=44.10253392,-81.14871575&sensor=true";
    URL url2 = new URL(imageUrl2);
    frame.add( new JLabel( new ImageIcon( new ImageIcon(url2).getImage().getScaledInstance( 630, 600, java.awt.Image.SCALE_SMOOTH ) ) ) );

    frame.setVisible(true);
    frame.pack();
    } catch (IOException e) {
        e.printStackTrace();
    System.exit(1);
    }

Second part: Google has THIS tutorial to display maps.

My question: I need to parse a text file in order to get the GPS coordinates. Text parsing is easy in Java. Considering that text parsing has to be done apart from displaying the map, which of the 2 parts mentioned above is a better way to display maps? I am a newbie in this, hence the question.

Thanks.

First part: your example code forms a SECOND map, its not adding another location to the SAME map.

You should read the documentation for how to add multiple markers to one map https://developers.google.com/maps/documentation/staticmaps/#Markers ... you should end up with all the coordinates in ONE map URL.

Second part: That's a link to the JAVASCRIPT Maps API. You use that to display a map in a public web-page in a browser. Its mostly possible to use an application, but its not trivial. Using the static maps api is much easier.

You should also read the Terms, to make sure you are entitled to use the Maps API. The FAQ, provides a useful overview https://developers.google.com/maps/faq#tos https://developers.google.com/maps/faq#tos_nonweb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM