2016-08-10 6 views
0

Googleマップから他のアクティビティに値を渡したいと思います。今まではJSONパーサを使って値を取得し、Google Mapに挿入しました。しかし、今私は、 "Infowindow"をクリックすると "ID"を他のアクティビティに渡したいと思っています。どうすればこれを達成できますか?Googleマップから他のアクティビティに値を渡すには?

public class MainActivity extends FragmentActivity { 
    String titlee, snipett, ii,a; 
    Double latitude, longitude; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //*** Permission StrictMode 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 

     ArrayList<HashMap<String, String>> location = null; 
     String url = "http://xxxxxxxxx/xxxxxx/krishi.php"; 
     try { 

      JSONArray data = new JSONArray(request(url)); 

      location = new ArrayList<HashMap<String, String>>(); 
      HashMap<String, String> map; 

      for(int i = 0; i < data.length(); i++){ 
       JSONObject c = data.getJSONObject(i); 

       map = new HashMap<String, String>(); 
       map.put("LocationID", c.getString("id")); 
       map.put("Latitude", c.getString("latitude")); 
       map.put("Longitude", c.getString("longitude")); 
       map.put("Type", c.getString("listed_name")); 
       map.put("Title", c.getString("title")); 
       location.add(map); 

      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap(); 

     latitude = Double.parseDouble(location.get(0).get("Latitude")); 
     longitude = Double.parseDouble(location.get(0).get("Longitude")); 
     LatLng coordinate = new LatLng(latitude, longitude); 
     googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 12)); 

     for (int i = 0; i < location.size(); i++) { 
      latitude = Double.parseDouble(location.get(i).get("Latitude")); 
      longitude = Double.parseDouble(location.get(i).get("Longitude")); 
      final String name = location.get(i).get("Title"); 
      String sel = location.get(i).get("Type"); 
      a = location.get(i).get("LocationID"); 

      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.position(new LatLng(latitude, longitude)).title(name).snippet(sel); 
      Marker marker = googleMap.addMarker(markerOptions); 
      marker.showInfoWindow(); 

     } 

     googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 



      @Override 
      public View getInfoWindow(Marker arg0) { 
       return null; 
      } 


      @Override 
      public View getInfoContents(Marker arg0) { 
       View v = getLayoutInflater().inflate(R.layout.custom_info_window, null); 


       titlee = arg0.getTitle(); 
       snipett = arg0.getSnippet(); 
       TextView tvLat = (TextView) v.findViewById(R.id.title); 
       TextView tvLng = (TextView) v.findViewById(R.id.snippet); 
       tvLat.setText("Area Name: " + titlee); 
       tvLng.setText("Type: " + snipett); 

       return v; 

      } 

     }); 


     googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 
      @Override 
      public void onInfoWindowClick(Marker marker) { 





      } 
     }); 



    } 

    private String request(String urlString) { 
     // TODO Auto-generated method stub 

     StringBuilder chaine = new StringBuilder(""); 
     try{ 
      URL url = new URL(urlString); 
      HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
      connection.setRequestProperty("User-Agent", ""); 
      connection.setRequestMethod("POST"); 
      connection.setDoInput(true); 
      connection.connect(); 

      InputStream inputStream = connection.getInputStream(); 

      BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream)); 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
       chaine.append(line); 
      } 

     } catch (IOException e) { 
      // writing exception to log 
      e.printStackTrace(); 
     } 

     return chaine.toString(); 
    } 

} 
+0

その[緊急の指定](http://meta.stackoverflow.com/qに注意してください/ 326569/472495)はここではよく受信されません。この種のメッセージを省略するように精神的注意を払うことができれば、ここでボランティアはそれを感謝します。 – halfer

答えて

0

IDを取得し、同じようSecondScreenでそれを取得

Intent i = new Intent(FirstScreen.this, SecondScreen.class); 
i.putExtra("Id", intId); 
startActivity(i); 
を通じて、新たな活動に渡す:

Bundle extras = intent.getExtras(); 
    if(extras != null) 
    Int id = extras.getInt("Id"); // retrieve the data using keyName 
+0

ユーザーがインフォボタンをクリックしたときに「ID」を取得するにはどうすればよいですか? –

0

私はこの方法を行っている:

は、変数を初期化します。

private GoogleMap mMap; 
private HashMap<Marker, Integer> mHashMap = new HashMap<Marker,Integer>(); 
private ArrayList<MyCustomModelClass> myList = new ArrayList<MyCustomModelClass>(); 

あなたのArrayListを使用して、Googleマップ上にマーカーを追加:マーカーをクリックリスナーで

for (int i = 0; i < myList.size(); i++) { 
double latitude = myList.getLatitude(); 
double longitude = myList.getLongitude(); 
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))).title(myList.getTitle()) 
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)); 
mHashMap.put(marker, i); 

} 

を:

@Override 
public boolean onMarkerClick(Marker marker) { 
    int pos = mHashMap.get(marker); 
    Log.i("Position of arraylist", pos+""); 
} 
関連する問題