2017-11-27 6 views
0

현재 위도와 경도가 높은 응용 프로그램이 있습니다. 대신에 나는 포함 된 텍스트 파일이라고 cities.text에서 위도와 긴을 읽고 싶은 내가 구현하기 어려운이 기능을 찾는거야으로 도움을 사용할 수텍스트 파일에서 위도와 경도 읽기

Dublin 53.347860 -6.272487 
Kerry 52.264007 -9.686990 
Cork 51.892171 -8.475068 

을 다음 누군가가 나를 도울 수 있을까요 어떻게 끝났어?

여기까지 내가 지금까지 가지고있는 코드가 있습니다. 당신이 당신의 자산이 파일이 가정

지도 활동

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback { 

String city = ""; 
MapFragment mf; 
GoogleMap map; 

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

    Intent I = getIntent(); 
    city = I.getStringExtra("city"); 
    mf = (MapFragment) getFragmentManager().findFragmentById(R.id.the_map); 
    mf.getMapAsync(this); 
} 

@Override 
public void onMapReady(GoogleMap map) { // map is loaded but not laid out yet 
    this.map = map; 
    // code to run when the map has loaded 

    double lat, lon; 

    if (city.equals("Dublin")) 
    { 
     lat = 53.347860; 
     lon = -6.272487; 
    } 
    else if (city.equals("Kerry")) 
    { 
     lat= 52.264007; 
     lon= -9.686990; 

    } 
    else if (city.equals("Cork")) 
    { 
     lat= 51.892171; 
     lon= -8.475068; 
    } 
    else 
    { 
     lat=0; 
     lon=0; 
    } 

    map.addMarker(new MarkerOptions() 
      .position(new LatLng(lat, lon)) 
      .title("") 
    ); 

    map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon))); 
    map.moveCamera(CameraUpdateFactory.zoomTo(10)); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_map, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

CityListFragment

public class CityListFragment extends Fragment { 



public CityListFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_city_list, container, false); 

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_list_item_1, Arrays.asList("myLocation","Dublin","Kerry","Cork")); 
    ListView list = (ListView) view.findViewById(R.id.listView); 
    list.setAdapter(adapter); 

    list.setOnItemClickListener(
      new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> list, View row, int index, long rowID) { 
        // code to run when user clicks that item 
        // launch new Activity with holes details 
        ((CityMap)getActivity()).setCity((String)list.getItemAtPosition(index)); 
        ((CityMap)getActivity()).showMap(); 

       } 
      } 
    ); 
    return view; 
} 


} 

CityMap

public class CityMap extends AppCompatActivity implements OnMapReadyCallback { 

String city = ""; 
MapFragment mf; 
GoogleMap map; 
double latitude; 
double longitude; 

public void setCity(String city) 
{ 
    this.city = city; 
} 

public void showMap() 
{ 
    mf = (MapFragment) getFragmentManager().findFragmentById(R.id.the_map); 
    if (mf == null) { 
     // CityMapFragment (Fragment B) is not in the layout (handset layout), 
     // so start MapActivity (Activity B) 
     // and pass it the info about the selected item 
     Intent intent = new Intent(this, MapActivity.class); 
     intent.putExtra("city", city); 
     startActivity(intent); 
    } else { 
     // CityMApFragment (Fragment B) is in the layout (tablet layout) 
     mf.getMapAsync(this); 

    } 
} 

@Override 
public void onMapReady(GoogleMap map) { // map is loaded but not laid out yet 
    this.map = map; 
    // code to run when the map has loaded 

    double lat, lon; 
    if (city.equals("myLocation")) 
    { 
     lat= latitude; 
     lon= longitude; 

    } 

    else if (city.equals("Dublin")) 
    { 
     lat = 53.347860; 
     lon = -6.272487; 
    } 
    else if (city.equals("Kerry")) 
    { 
     lat= 52.264007; 
     lon= -9.686990; 

    } 

    else if (city.equals("Cork")) 
    { 
     lat= 51.892171; 
     lon= -8.475068; 
    } 
    else 
    { 
     lat=0; 
     lon=0; 
    } 

    map.addMarker(new MarkerOptions() 
      .position(new LatLng(lat, lon)) 
      .title("") 
    ); 

    map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon))); 
    map.moveCamera(CameraUpdateFactory.zoomTo(10)); 

} 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_city_map, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 
+0

어리석은 양의 코드를 버렸습니다. 파일에서 정보를 읽는 것과는 무관합니다. – greenapps

답변

0

은 위의 형식으로 위도-LNG 데이터와 폴더, 당신 이 기능을 사용할 수 있습니다.

private HashMap<String, Location> readFile(String path){ 
     HashMap<String, Location> citiesLoactionMap = new HashMap<>(); 

     String dump = null; 
     try { 
      dump = readFromAssets(mContext, path); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (dump != null && !dump.isEmpty()) { 

      String[] cities = dump.split("\n"); 
      for(String city : cities){ 

       String[] cityData = city.split("\\s+"); 
       if(cityData[0] != null && cityData[1] != null && cityData[2] != null){ 

        Location location = new Location(cityData[0]); 
        location.setLatitude(Double.parseDouble(cityData[1])); 
        location.setLongitude(Double.parseDouble(cityData[2])); 
        citiesLoactionMap.put(cityData[0], location); 

       } 

      } 

     } 
     return citiesLoactionMap; 
    } 

여기서 경로는 파일 이름입니다 (예 : "cities.txt"

+0

고마워!. 이 코드를 정확히 어디에 넣어야합니까? –