2016-07-25 61 views
0

因爲我會使用put extra從其他類抓取數據。我怎樣才能從另一類獲得經度和緯度的價值。我曾嘗試下面的代碼:如何LatLng從另一個課程?

package com.example.khoir.emegency_service; 

Bundle extras =getIntent().getExtras(); 
Double lat1=Double.valueOf(extras.getString("lt1")); 
Double lg1=Double.valueOf(extras.getString("lg2")); 

LatLng fromPosition = new LatLng(lat1, lg1); 
LatLng toPosition = new LatLng(-7.8032857, 110.3738408); 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync((OnMapReadyCallback) this); 


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

    md = new GMapV2Direction(); 
    mMap = ((MapFragment) getFragmentManager() 
      .findFragmentById(R.id.map)).getMap(); 

    LatLng coordinates = new LatLng(-7.8032857, 110.3738408); 
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16)); 


    mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start")); 
    mMap.addMarker(new MarkerOptions().position(toPosition).title("End")); 

    Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_WALKING); 


    ArrayList<LatLng> directionPoint = md.getDirection(doc); 
    PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED); 

    for (int i = 0; i < directionPoint.size(); i++) { 
     rectLine.add(directionPoint.get(i)); 
    } 

    mMap.addPolyline(rectLine); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 


} 

package com.example.khoir.emegency_service; 
DialogInterface.OnClickListener listener; 

@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button go = (Button) findViewById(R.id.button1); 
    go.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(MainActivity.this, Main_Pemadam_Kebakaran.class); 
      startActivity(i); 
     } 
    }); 

    Button kul = (Button) findViewById(R.id.button2); 
    kul.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(MainActivity.this, MainRumahSakit.class); 
      startActivity(i); 
     } 
    }); 



    final Button maps = (Button) findViewById(R.id.button4); 
    maps.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 



      Intent i = new Intent(MainActivity.this, MapsActivity.class); 
      i.putExtra("lt1",""+"-7.8013823"); 
      i.putExtra("lg1",""+"110.3647725"); 





      startActivity(i); 
     } 
    }); 
    Button peng = (Button) findViewById(R.id.button3); 
    peng.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(MainActivity.this, MainPolisi.class); 
      startActivity(i); 
     } 
    }); 
    Button tent = (Button) findViewById(R.id.button5); 
    tent.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(MainActivity.this, Tentang.class); 
      startActivity(i); 
     } 
    }); 

    Button twit= (Button) findViewById(R.id.btntwitter); 
    twit.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(MainActivity.this, twitter.class); 
      startActivity(i); 
     } 
    }); 

    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Aplikasi Memerlukan Akses GPS, GPS Anda Belum Aktif, Buka Setting Dan Aktifkan GPS?") 
       .setCancelable(false) 
       .setPositiveButton("Ya", new DialogInterface.OnClickListener() { 
        public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
         startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
        } 
       }) 
       .setNegativeButton("Tidak", new DialogInterface.OnClickListener() { 
        public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
         dialog.cancel(); 
        } 
       }); 
     final AlertDialog alert = builder.create(); 
     alert.show(); 
    } else { 
     Toast.makeText(getApplicationContext(), "GPS Sudah Aktif", Toast.LENGTH_LONG).show(); 
    } 
} 

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    //jika tombol BACK ditekan 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     Keluar(); 
    } 
    return super.onKeyDown(keyCode, event); 
} 

//method untuk keluar aplikasi menggunakan dialog terlebih dahulu 
private void Keluar() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Apakah Anda Yakin Ingin Keluar?"); 
    builder.setCancelable(false);//tombol BACK tidak bisa tekan 

    //Membuat listener untuk tombol DIALOG 
    listener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      if (which == DialogInterface.BUTTON_POSITIVE) { 
       finish(); //keluar aplikasi 
      } else if (which == DialogInterface.BUTTON_NEGATIVE) { 
       dialog.cancel(); //batal keluar 
      } 
     } 
    }; 

    //menerapkan listener pada tombol ya dan tidak 
    builder.setPositiveButton("Ya", listener); 
    builder.setNegativeButton("Tidak", listener); 
    builder.show(); //menampilkan dialog 


} 
+0

從 最終按鈕映射=(按鈕)findViewById(R.id.button4); maps.setOnClickListener(新View.OnClickListener(){ 公共無效的onClick(視圖v){ 意圖I =新意圖(MainActivity.this,MapsActivity.class); i.putExtra( 「LT1」,」 「+」 - 7.8013823 「); i.putExtra(」 LG1" , 「」 + 「110.3647725」); startActivity(ⅰ); } }); –

回答

0

在這裏,在第一個活動,你在字符串不「捆綁」的形式發送值。 所以在第二個活動使用像

 String lat1=getIntent().getStringExtra("lat1"); 
String lat1=getIntent().getStringExtra("lat2"); 

然後轉換成雙。

+0

String lat1 = getIntent()。getStringExtra(「lat1」); String lg1 = getIntent()。getStringExtra(「lat2」); Double isi1 = Double.parseDouble(lat1); Double isi2 = Double.parseDouble(lg1); LatLng fromPosition = new LatLng(isi1,isi2); LatLng toPosition = new LatLng(-7.8032857,110.3738408); 不工作的兄弟請幫忙... –

+0

我沒有得到錯誤,但試試這對我有用.... –

0

你可以像這樣發送latLng: - intent.putExtra(「」keyname1「,lat); intent.putExtra(」「keyname2」,lng);

而在接收器類中: - Double lat = ggetIntent()。getDoubleExtra(「keyname1」,default value); Double lng = ggetIntent()。getDoubleExtra(「keyname2」,默認值);

0

我沒有錯誤,但嘗試這個工作對我來說....

double latitude =26.8006704 ; 
double longitude =75.7965136; 
// Google Map 
private GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    try { 
     // Loading map 
     initilizeMap(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
/** 
* function to load map. If map is not created it will create it for you 
* */ 
private void initilizeMap() 
{ 
    if (googleMap == null) 
    { 
     //getting a reference of GoogleMap class 
     FragmentManager fm=(FragmentManager)getFragmentManager(); 
     MapFragment mf =(MapFragment)fm.findFragmentById(R.id.map); 
     googleMap =mf.getMap(); 
      //displaying current location on the map 
      googleMap.setMyLocationEnabled(true); // false to disable 
      //set map type 
      googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      //googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
     // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
     // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE); 


      // create add marker on the map 
      MarkerOptions marker = new MarkerOptions(); 
      LatLng latlan=new LatLng(latitude, longitude); 
      marker.position(latlan); 
      marker.title("City name"); 
      // adding marker 
      googleMap.addMarker(marker); 
      //to change the marker color 
      BitmapDescriptor discriptor=BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED); 
      marker.icon(discriptor); 


      //Moving Camera to a Location with animation 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude,longitude)).zoom(9).build(); 

    googleMap.animateCamera(CameraUpdateFactory. 
    newCameraPosition(cameraPosition));}} 
相關問題