The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          import java.io.BufferedReader; 
          import java.io.InputStream; 
          import java.io.InputStreamReader; 
          import java.net.URL; 
          import java.util.Date; 
            
          import org.apache.http.HttpEntity; 
          import org.apache.http.HttpResponse; 
          import org.apache.http.client.methods.HttpPost; 
          import org.apache.http.entity.StringEntity; 
          import org.apache.http.impl.client.DefaultHttpClient; 
          import org.json.JSONArray; 
          import org.json.JSONObject; 
            
          import android.app.Activity; 
          import android.content.Context; 
          import android.os.Bundle; 
          import android.telephony.TelephonyManager; 
          import android.telephony.gsm.GsmCellLocation; 
          import android.view.View; 
          import android.widget.Button; 
          import android.widget.TextView; 
            
          public class LocationStation extends Activity 
              TextView mTextView; 
              Button mButton; 
              TelephonyManager tm; 
            
              
          /** Called when the activity is first created. */
              @Override
              
          public void onCreate(Bundle savedInstanceState) 
                  
          super.onCreate(savedInstanceState); 
                  setContentView(R.layout.main); 
            
                  mTextView 
          = (TextView) findViewById(R.id.textView); 
                  mButton 
          = (Button) findViewById(R.id.Button); 
                  tm 
          = (TelephonyManager) this
                          .getSystemService(Context.TELEPHONY_SERVICE); 
            
                  mButton.setOnClickListener(
          new Button.OnClickListener() 
            
                      @Override
                      
          public void onClick(View v) 
                          
          // TODO Auto-generated method stub 
                          GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); 
                          
          int cid = gcl.getCid();  
                          
          int lac = gcl.getLac(); 
                          System.out.println(
          "operator"+tm.getNetworkOperator()); //中國移動43600 
                          int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0
                                  
          3)); 
                          
          int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3
                                  
          5)); 
                          
          /* 
                          * 發送的格式:{ 
                                      "version": "1.1.0" , 
                                      "host": "maps.google.com", 
                                      "access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe", 
                                      "home_mobile_country_code": 460, 
                                      "home_mobile_network_code":0, 
                                      "address_language": "zh_CN", 
                                      "radio_type": "gsm", 
                                      "request_address": true , 
                                      "cell_towers":[ 
                                      { 
                                      "cell_id":36526, 
                                      "location_area_code":14556, 
                                      "mobile_country_code":460, 
                                      "mobile_network_code":0, 
                                      "timing_advance":5555 
                                      } 
                                      ] 
                                      } 
                           
          */
           
                          
          try 
                              
          // 組裝JSON查詢字符串 
                              JSONObject holder = new JSONObject(); 
                              holder.put(
          "version""1.1.0"); 
                              holder.put(
          "host""maps.google.com"); 
                              holder.put(
          "address_language""zh_CN"); 
                              holder.put(
          "request_address"true); 
            
                              JSONArray array 
          = new JSONArray(); 
                              JSONObject data 
          = new JSONObject(); 
                              data.put(
          "cell_id", cid); // 25070 
                              data.put("location_area_code", lac);// 4474 
                              data.put("mobile_country_code", mcc);// 460 
                              data.put("mobile_network_code", mnc);// 0 
                              array.put(data); 
                              holder.put(
          "cell_towers", array); 
            
                              
          // 創建連接,發送請求并接受回應 
                              DefaultHttpClient client = new DefaultHttpClient(); 
            
                              HttpPost post 
          = new HttpPost( 
                                      
          "http://www.google.com/loc/json"); 
            
                              StringEntity se 
          = new StringEntity(holder.toString()); 
            
                              post.setEntity(se); 
                              HttpResponse resp 
          = client.execute(post); 
            
                              HttpEntity entity 
          = resp.getEntity(); 
            
                              BufferedReader br 
          = new BufferedReader( 
                                      
          new InputStreamReader(entity.getContent())); 
                              StringBuffer sb 
          = new StringBuffer(); 
                              String result 
          = br.readLine(); 
            
                              
          while (result != null
            
                                  sb.append(result); 
                                  result 
          = br.readLine(); 
                              }
           
                              
          /* 
                               * 返回格式:   { 
                                    "location": { 
                                      "latitude": 51.0, 
                                      "longitude": -0.1, 
                                      "altitude": 30.1, 
                                      "accuracy": 1200.1, 
                                      "altitude_accuracy": 10.1, 
                                      "address": { 
                                        "street_number": "100", 
                                        "street": "Amphibian Walkway", 
                                        "postal_code": "94043", 
                                        "city": "Mountain View", 
                                        "county": "Mountain View County", 
                                        "region": "California", 
                                        "country": "United States of America", 
                                        "country_code": "US" 
                                      } 
                                    } 
                                  } 
                               
          */

                              JSONObject jsonObject 
          = new JSONObject(sb.toString()); 
            
                              JSONObject jsonObject1 
          = new JSONObject(jsonObject 
                                      .getString(
          "location")); 
            
                              getAddress(jsonObject1.getString(
          "latitude"), jsonObject1 
                                      .getString(
          "longitude")); 
            
                              
          //mTextView.setText(sb.toString()); 
                          }
           catch (Exception e) 
                              
          // TODO: handle exception 
                          }
           
            
                      }
           
            
                  }
          ); 
              }
           
            
              
          void getAddress(String lat, String lag) 
                  
          try 
            
                      URL url 
          = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="
                              
          + lat + "," + lag); 
                      InputStream inputStream 
          = url.openConnection().getInputStream(); 
                      InputStreamReader inputReader 
          = new InputStreamReader(inputStream, 
                              
          "utf-8"); 
                      BufferedReader bufReader 
          = new BufferedReader(inputReader); 
            
                      String line 
          = "", lines = ""
            
                      
          while ((line = bufReader.readLine()) != null
                          lines 
          += line; 
                      }
           
                      
          if (!lines.equals("")) 
            
                          JSONObject jsonobject 
          = new JSONObject(lines); 
                          JSONArray jsonArray 
          = new JSONArray(jsonobject.get("Placemark"
                                  .toString()); 
                          
          for (int i = 0; i < jsonArray.length(); i++
            
                              mTextView.setText( jsonArray.getJSONObject(i).getString(
          "address")); 
                                        
                          }
           
            
                      }
           
            
                  }
           catch (Exception e) 
                      ; 
                  }
           
            
              }
           
          }
          posted on 2012-04-23 14:48 Eric_jiang 閱讀(2251) 評論(2)  編輯  收藏 所屬分類: Android

          Feedback

          # re: Android 基于GeolocationAPI的基站定位 2012-11-06 14:53 admin
          http://code.google.com/p/gears/wiki/GeolocationAPI
          The Google Gears Geolocation API will stop responding to requests on November 17, 2012.
          11.17.2012就關閉了  回復  更多評論
            

          # re: Android 基于GeolocationAPI的基站定位 2012-11-26 20:07 passerbywhu
          是的,已經關閉了。。。@admin
            回復  更多評論
            

          主站蜘蛛池模板: 大洼县| 泽州县| 拉萨市| 洛隆县| 花垣县| 古田县| 江孜县| 克东县| 资阳市| 团风县| 三江| 绥滨县| 嫩江县| 青阳县| 正阳县| 奎屯市| 江津市| 清远市| 九龙县| 宝山区| 天峻县| 南城县| 红桥区| 普宁市| 大同县| 沐川县| 舒兰市| 安宁市| 巴林左旗| 赤城县| 日喀则市| 新巴尔虎右旗| 祁连县| 光泽县| 红河县| 子长县| 河西区| 湘西| 石河子市| 台南县| 海原县|