2016-09-22 167 views
0

我的字符串格式是">RPV21305+1134038+7772277800000012;ID=0011<" 我分離經緯度,如下面的代碼。但是我無法通過谷歌API訪問緯度和經度的地址。我的程序需要反向地理代碼。谷歌反向地理編碼從經緯度得到地址

我的計劃是

import java.io.*; 
import java.net.*; 
import java.util.*; 
import java.sql.*; 
import java.sql.Statement; 
import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.text.SimpleDateFormat; 

public class FileHandler { 
    public static void main(String [] args) { 
     //reading a file for iput 
     String fileName = "File1/ascii_file"; 
     String line = null; 
     try { 
      FileReader fileReader = new FileReader(fileName); 
      BufferedReader bufferedReader = new BufferedReader(fileReader); 
      while((line = bufferedReader.readLine()) != null) { 
       System.out.println(line); 
       int se = line.endsWith("<")? (line.length() - 1) : line.length(); 
       System.out.println("se-----------------------------"+se); 
       String parsedstring = line.substring(1,se); 
       System.out.println("parsedString"+parsedstring); 
       String T[] = parsedstring.split(";"); 
       System.out.println("ttttt-----------------------------"+T[0]); 
       String mobileID = null; 
       for (int i = 1; i < T.length; i++) { 
        if (T[i].startsWith("ID=")) { 
         mobileID = T[i].substring(3); 
         System.out.println("mobileID-----------------"+mobileID); 
         break; 
        } 
       } 

       //getting lattitude and longitude from string; 
       int gpsTOD  = Integer.parseInt(T[0].substring(
       float latitude = Float.parseFloat(T[0].substring(8 
       float lat  =latitude/100000; 
       float longitude = Float.parseFloat(T[0].substring(16,25)); 
       float lon   =longitude/1000000; 
       float speedKPH = Float.parseFloat(T[0].substring(25,28)); 
       float headingDeg = Float.parseFloat(T[0].substring(28,31)); 
       String srcStr  = T[0].substring(31,32); 
       String ageStr  = T[0].substring(32,33); 
       double altitudeM = 0.0; 
       double odomKM  = 0.0; 
       long gpioInput = 0L; 
       String accountID ="demo"; 
       String status  ="0"; 
       System.out.println("gpstod:"+gpsTOD); 
       System.out.println("lat:"+lat); 
       System.out.println("lon:"+lon); 
       System.out.println("speedKPH:"+speedKPH); 
       System.out.println("headingDeg:"+headingDeg); 
       System.out.println("srcStr:"+srcStr); 
       System.out.println("ageStr:"+ageStr); 
       System.out.println("altitudeM:"+altitudeM); 
       System.out.println("odomKM:"+odomKM); 
       System.out.println("gpioInput:"+gpioInput); 
       try { 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gts","gts","opengts"); 
        Statement stmt=con.createStatement(); 
        stmt.executeUpdate("insert into EventData(accountID,deviceID,timestamp,statusCode,latitude,longitude,gpsAge,speedKPH,heading,altitude,odometerKM)values('"+ accountID +"','"+ mobileID +"','"+ gpsTOD +"','"+ status +"','"+ lat +"','"+ lon +"','"+ ageStr +"','"+ speedKPH +"','"+ headingDeg +"','"+ altitudeM +"','"+ odomKM +"')"); 
        System.out.println("insert into EventData(accountID,deviceID,timestamp,latitude,longitude,gpsAge,speedKPH,heading,altitude,odometerKM)values('"+ accountID +"','"+ mobileID +"','"+ gpsTOD+"','"+ lat +"','"+ lon +"','"+ ageStr +"','"+ speedKPH +"','"+ headingDeg +"','"+ altitudeM +"','"+ odomKM +"')"); 
        con.close(); 
       } 
       catch (Exception e) { 
        System.out.println(e); 
       } 
      } 
      bufferedReader.close(); 
     } 
     catch(FileNotFoundException ex) { 
      System.out.println("Unable to open file '" + fileName + "'"); 
     } 
     catch(IOException ex) { 
      System.out.println("Error reading file '" + fileName + "'"); 
     } 
    } 
} 

回答

1

你需要像這樣的代碼

GeoApiContext gtx = new GeoApiContext().setApiKey("**<<<<your-key-here>>>>**"); 
try { 
    GeocodingResult[] gResp = GeocodingApi.newRequest(gtx).latlng(new LatLng(37, -121)).await(); 
    System.out.println(gResp[0].formattedAddress); 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 

同時確保谷歌的API是從控制檯啓用,你有你的關鍵。有了這兩個你應該得到的輸出如下所示,

信息:請求:https://maps.googleapis.com/maps/api/geocode/json?key=你的鑰匙 &經緯度= 37.38000000%2C-121.88000000 1685萊德勒CIR,聖何塞,加利福尼亞州95131,USA

在其下方的地址解析API是HTTP請求,所以如果你願意,你可以嘗試伸直以及

類似下面也應努力

https://maps.googleapis.com/maps/api/geocode/json?latlng=23,82.5 

要在罐子您的評論添加以下的依賴,如果你使用的是Java代碼

<dependency> 
    <groupId>com.google.maps</groupId> 
    <artifactId>google-maps-services</artifactId> 
    <version>0.1.15</version> 
</dependency> 

這幾乎在以下進口

import com.google.maps.GeoApiContext; 
import com.google.maps.GeocodingApi; 
import com.google.maps.GeocodingApiRequest; 
import com.google.maps.model.GeocodingResult; 
import com.google.maps.model.LatLng; 
+0

多數民衆贊成罰款,我使用的Web應用程序,我不能訪問包GeoApiContext,GeocodingResult,GeocodingApi。你能告訴我什麼packege我必須使用 – vignesh

+0

它是android代碼?還是java web應用 – vignesh

+0

以上是java代碼示例 –

相關問題