2016-09-02 7 views
-2

私はアンドロイドのGoogleマップアプリケーションを開発しています。これは地図の現在の位置を示しています。マーカーの緯度と経度の位置を知り、ポリラインを描く方法

enter image description here

私は、ユーザーが任意のエリア名を入力すると、その後、第2のマーカーがその位置に配置されます、アプリケーション内の検索バーを持っています。

enter image description here

今私の問題は、第2のマーカーの経度と緯度の位置を取得し、二つのマーカー間のルートを作成する方法、です。

私MainActivity.javaコードを次のように

public class MainActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    public void onMapSearch(View view) { 
     EditText locationSearch = (EditText) findViewById(R.id.editText); 
     String location = locationSearch.getText().toString(); 
     List<Address> addressList = null; 

     if (location != null || !location.equals("")) { 
      Geocoder geocoder = new Geocoder(this); 
      try { 
       addressList = geocoder.getFromLocationName(location, 1); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Address address = addressList.get(0); 
      LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
      mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 
      mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
     } 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(27.746974, 85.301582); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Kathmandu, Nepal")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     // Enable MyLocation Button in the Map 
     mMap.setMyLocationEnabled(true); 
    } 
} 

私を助けてください。

+0

チェックソリューションをそこから値を取得し、アドレスとしてそれを渡します。 //stackoverflow.com/questions/26112591/draw-path-between-two-points-on-the-map-google-maps-using-eclipse/26113218#26113218 – Namy

+0

答えを詳しく教えてください。 –

+0

i don ' 2番目のマーカーの緯度と経度の位置、どのように私のコードからこれらの値をフェッチする.. –

答えて

0

プログラムでは、コードを使用して2番目のマーカーのLatLngを取得しています。

LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 

ポリラインを作成するには、このリンクを参照してください。この質問によって既に回答されています。

https://www.simplifiedcoding.net/google-maps-distance-calculator-google-maps-api/

+0

私はすでにそのコードを持っています。 –

+0

http://stackoverflow.com/questions/38583015/how-to-calculate-distance-and-duration-for-multiple-locations-using-distance-mat/38583719#38583719 –

+0

私にはDirectionsJSONParser.javaファイルがあります。あなたのコード。 –

0

検索EdiTextためGoogle Places APIを設定します。あなたはHTTP あなたの場所との間のパスを描きたい場合は、ここでここで

(ここではstrAddressを表すaddress)コード..です

List<Address> address = null; 
    Geocoder coder = new Geocoder(getApplicationContext()); 
    try { 
     address = coder.getFromLocationName(strAddress, 1); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (address != null) { 
     Address location = address.get(0); 
     double lat = location.getLatitude(); 
     double log = location.getLongitude(); 
    } 
関連する問題