在Android中使用谷歌地图搜索地图

时间:2012-12-01 11:16:15

标签: android google-maps

大家

我的项目有问题

这是我的search.java代码

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Search extends Activity {

   TextView error,br2;
   Spinner namjal2;
   String i,returnString2,jln;
   String[] jalan = {"A.Yani", "Ambengan", "Anjasmoro","Baliwerti","Basuki Rahmat","Bintoro","Ciliwung","Citarum","Citandui","Darmo Daya","Darmokali","Dharmawangsa","Dinoyo","Embong Kemiri","Embong Malang"};


    /** Called when the activity is first created. */

   @Override


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        br2=(TextView)findViewById(R.id.berita2);
        namjal2=(Spinner)findViewById(R.id.namjalSearch);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, jalan);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         namjal2.setAdapter(aa);




   }

   public void clickHandler(View view){
    Intent a = null;
    switch (view.getId()){
    case R.id.find:
        jln = jalan[namjal2.getSelectedItemPosition()];
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

postParameters.add(new BasicNameValuePair("nama_jalan", jln));


/*            String valid = "1";*/      

        String response = null;

        try {

           response = CustomHttpClient.executeHttpPost("http://192.168.1.3/android/searchInfo.php", postParameters);


           String result = response.toString();  
           //parse json data

              try{
                      returnString2 = "";


                JSONArray jArray = new JSONArray(result);
                      for(int i=0;i<jArray.length();i++){

                              JSONObject json_data = jArray.getJSONObject(i);
                              Log.i("log_tag","id_kepadatan: "+json_data.getInt("id_kepadatan")+
                                      ", username: "+json_data.getString("username")+
                                      ", nama_jalan: "+json_data.getString("nama_jalan")+
                                      ", status: "+json_data.getString("status")+
                                      ", tanggal: "+json_data.getString("tanggal")+
                                      ", waktu: "+json_data.getString("waktu")+
                                      ", keterangan: "+json_data.getString("keterangan")
                              );
                              if(result.equals(null)){
                              //Get an output to the screen
                             returnString2 += "Data tidak ada";
                              }
                              else{
                              returnString2 += "\n" + json_data.getString("nama_jalan") + " mengalami "+ json_data.getString("status")+ " pada "+ json_data.getString("tanggal")+ " waktu "+ json_data.getString("waktu")+ " karena "+ json_data.getString("keterangan");
                              }
                      }
              }


              catch(JSONException e){
                      Log.e("log_tag", "Error parsing data "+e.toString());
              }

              try{


               br2.setText(returnString2);


              }


              catch(Exception e){

               Log.e("log_tag","Error in Display!" + e.toString());;          

              }   

         }

               catch (Exception e) {

          Log.e("log_tag","Error in http connection!!" + e.toString());     

         }
        break;

   case R.id.back:
        a = new Intent(this, Menu.class);
        startActivity(a);
        break;
}
   }


}

这是我的search.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:background="#ffffff"  
   > 

  <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="10dip"
          >
  <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="PENCARIAN BERITA TERBARU"/>
    <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Masukkan Nama Jalan"/>

          <Spinner
                android:id="@+id/namjalSearch" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true"/>
    <Button android:id="@+id/find" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="250dip"
                android:text="Find"
                android:onClick="clickHandler"/>
    <TextView
                android:id="@+id/berita2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                />  
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
          <TextView
                android:id="@+id/textBack" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:layout_marginTop="130dip"
                android:layout_marginLeft="190dip"
                android:textColor="#372c24"
                android:text="BACK"/>
        <ImageButton
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="125dip"
                android:layout_marginLeft="258dip"
                android:src="@drawable/back"
                android:background="@null" 
                android:onClick="clickHandler"/>
        <Button android:id="@+id/cariPeta" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="125dip"
                android:text="Cari Peta"
                android:onClick="clickHandler"/>
            </RelativeLayout>
    </LinearLayout>
</RelativeLayout>
</ScrollView>

我的目标是当我点击“cariPeta”按钮时,它会显示选择街道的地图

例如,如果微调器指向街道的一个名称(例如:“A.Yani”街道)...那么,当我点击“cariPeta”按钮,,,它将显示“A.Yani”的地图谷歌地图中的街道

我的问题是,我不知道应该写什么代码....你能告诉我吗?我会非常感激,因为这个项目对我来说非常重要

谢谢.....

1 个答案:

答案 0 :(得分:1)

好的,让我指导你。 您需要编写按钮的单击事件。 单击该按钮的getSelectedIndex of spinner(在spinner obj中有一个现成的方法)。

从字符串数组中接收索引值获取文本。

然后地图就会出现。但要处理地图,你需要一对lat / long。

使用lat / long,您可以在地图上显示任意点。