2016-11-10 2 views
-1

Google지도를 사용하여 앱을 개발 중입니다. 여기에서 경도 위도 값이 양식 서버를 가져 오는 위치에 마커를 표시하려고합니다.이 가져온 경도 및 위도 값에서 마커를 표시하려면 어떻게해야합니까? 다음 코드는 내 서버에서 경도 및 위도 값을 성공적으로 가져오고,이 가져 오기 langitude 및 위도와 함께 Google지도에 마커를 표시하려고합니다. 어떻게해야합니까?안드로이드의 Google지도에서 주어진 위치에 마커를 어떻게 표시합니까?

//java code 
public class Location_Track1 extends Activity { 

    private static final String TAG = "Location_Track1"; 
    private static final long INTERVAL = 1000 * 60 * 1; //1 minute 
    private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute 

    private static final float SMALLEST_DISPLACEMENT = 0.25F; //quarter of a meter 
    Button btnFusedLocation; 
    TextView tvLocation; 
    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    Location mCurrentLocation; 
    String mLastUpdateTime; 
    GoogleMap googleMap; 
    private ImageView mCurrentPointer; 

    private double longitude; 
    private double latitude; 
// private ArrayList<LatLng> points; //added 
    private List<LatLng> points = new ArrayList<>(); 
    Polyline line; //added 

    TextView tv_mobno, tv_latitude, tv_longitude, tv_time; 
    String getLatitude; 
    String getLongitude; 
    Button slocation; 

    JSONArray result = null; 

    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    static String CON = "CON"; 
    JSONObject jsonobject; 
    JSONArray jsonarray; 

    private String url =""; 

    private static final String TAG_USER = "result"; 

    // private static final String TAG_SNAME = "pseats"; 
    private static final String TAG_LONG = "longitude"; 
    private static final String TAG_LAT = "latitude"; 
    private static final String TAG_ADDRESS = "paddress"; 

    protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(INTERVAL); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

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

     Log.d(TAG, "onCreate ..............................."); 

     tv_mobno=(TextView)findViewById(R.id.textView_mob); 
     tv_latitude=(TextView)findViewById(R.id.textView_latitude); 
     tv_longitude=(TextView)findViewById(R.id.textView_longitude); 
     tv_time=(TextView)findViewById(R.id.textView_time); 
     slocation=(Button)findViewById(R.id.button_slocation); 

     url = "http://example.in/gmap_track.php"; 

    // points = new ArrayList<LatLng>(); 


     googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 


     slocation.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       // new DownloadJSON().execute(); 
       new JSONParse().execute(); 
      } 
     }); 

    } 

    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      pDialog = new ProgressDialog(Location_Track1.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 

     } 

     @Override 
     protected JSONObject doInBackground(String... args) { 
      JSONParser jParser = new JSONParser(); 

      // Getting JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(url); 
      return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
      pDialog.dismiss(); 
      try { 

       // Getting JSON Array 
       result = json.getJSONArray(TAG_USER); 
       JSONObject c = result.getJSONObject(0); 


       // Storing JSON item in a Variable 
       String lat = c.getString(TAG_LAT); 
       String longt = c.getString(TAG_LONG); 


       //Set JSON Data in TextView 

       tv_latitude.setText(lat); 
       tv_longitude.setText(longt); 

      /* double lat = c.getDouble(TAG_LAT); 
       double longt = c.getDouble(TAG_LONG); 

       MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, longt)).title("point"); 
       googleMap.addMarker(marker);*/ 


      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    } 
} 
+0

은 어떻게 구글 맵 형태의 서버를 가져 오는 (위도, 경도)의 JSON 값을 보여 않는 제안 나를 anyne 수 있습니까? –

답변

1

당신은 마커를 추가하기위한 코드에서 주석 이유는 무엇입니까? 이 시도 :

// Storing JSON item in a Variable 
    String lat = c.getString(TAG_LAT); 
    String longt = c.getString(TAG_LONG); 


//Set JSON Data in TextView 

    tv_latitude.setText(lat); 
    tv_longitude.setText(longt); 

//Parse data to double 
    double latD = Double.parseDouble(lat); 
    double longtD = Double.parseDouble(longt); 

    MarkerOptions marker = new MarkerOptions().position(new LatLng(latD, longtD)).title("point"); 
    googleMap.addMarker(marker); 
+0

에서 위도/경도 값을 가져 와서 위의 값을 사용하여 위치를 표시하지 않습니다. 맵에 공백 만 표시됩니다. –

+0

https://developers.google.com/maps/documentation/android-api/start –

+0

가져온 langitude 및 latitude 값을 마커에 어떻게 표시합니까? –

0
@Override 
    public void onMapReady(GoogleMap googleMap) { 
     LatLng sydney = new LatLng(-33.867, 151.206); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); 
     googleMap.addMarker(new MarkerOptions().title("Sydney").snippet("The most populous city in Australia.").position(sydney)); 
    } 
+0

나는 주어진 값 (위도, 경도)에서 마커를 보여주고 싶습니다.이 값은 서버에서 가져옵니다. –

+0

Just Simple 위도/경도 값을 위도/경도 값으로 대체하여 서버 –

0

데모 클래스, 당신은 주어진 위도와 길이에 마커를 추가 할 수있는 사용.

public class MarkerDemoActivity extends FragmentActivity implements 
    OnMarkerClickListener, 
    OnMapReadyCallback { 

private static final LatLng PERTH = new LatLng(-31.952854, 115.857342); 
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689); 
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235); 

private Marker mPerth; 
private Marker mSydney; 
private Marker mBrisbane; 

private GoogleMap mMap; 

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

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

/** Called when the map is ready. */ 
@Override 
public void onMapReady(GoogleMap map) { 
    mMap = map; 

    // Add some markers to the map, and add a data object to each marker. 
    mPerth = mMap.addMarker(new MarkerOptions() 
      .position(PERTH) 
      .title("Perth"); 
    mPerth.setTag(0); 

    mSydney = mMap.addMarker(new MarkerOptions() 
      .position(SYDNEY) 
      .title("Sydney"); 
    mSydney.setTag(0); 

    mBrisbane = mMap.addMarker(new MarkerOptions() 
      .position(BRISBANE) 
      .title("Brisbane"); 
    mBrisbane.setTag(0); 

    // Set a listener for marker click. 
    mMap.setOnMarkerClickListener(this); 
} 

/** Called when the user clicks a marker. */ 
@Override 
public boolean onMarkerClick(final Marker marker) { 

    // Retrieve the data from the marker. 
    Integer clickCount = (Integer) marker.getTag(); 

    // Check if a click count was set, then display the click count. 
    if (clickCount != null) { 
     clickCount = clickCount + 1; 
     marker.setTag(clickCount); 
     Toast.makeText(this, 
         marker.getTitle() + 
         " has been clicked " + clickCount + " times.", 
         Toast.LENGTH_SHORT).show(); 
    } 

    // Return false to indicate that we have not consumed the event and that we wish 
    // for the default behavior to occur (which is for the camera to move such that the 
    // marker is centered and for the marker's info window to open, if it has one). 
    return false; 
} 

은}

관련 문제