2014-05-11 158 views
0

座標這是我TrackMap.java谷歌地圖標記,並在Android的

public class TrackMap extends FragmentActivity implements LocationListener { 

    GoogleMap map; 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_track_map); 

     boolean lowPowerMoreImportantThanAccurancy = true; 
     LocationRequest request = LocationRequest.create(); 
     request.setPriority(lowPowerMoreImportantThanAccurancy ? 
       LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY: 
       LocationRequest.PRIORITY_HIGH_ACCURACY); 

     map = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map)).getMap(); 

     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

     // Enabling MyLocation Layer of Google Map 
     map.setMyLocationEnabled(true); 

     Marker marker = map.addMarker(new MarkerOptions() 
       .position(new LatLng(0, 0)).draggable(true) 
       // Set Opacity specified as a float between 0.0 and 1.0, 
       // where 0 is fully transparent and 1 is fully opaque. 
       .alpha(0.7f).flat(true)); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     map.clear(); 

     MarkerOptions mp = new MarkerOptions(); 

     mp.position(new LatLng(location.getLatitude(), location.getLongitude())); 
     mp.title("Current Position"); 

     map.addMarker(mp); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 


} 

我在這裏的問題是我應該怎麼做才能讓用戶添加標記在地圖上,我的TrackMap.java能夠得到協調和保存在一個變量中。我應該怎麼做?

從我在網上搜索,他們只是教與已知的座標標記。但是現在我想讓用戶在不知道座標的情況下添加標記。

+0

如果可以提供一些源代碼來添加標記而不知道座標(此代碼發生在用戶添加標記之後)會更好。 –

回答

1

如何在用戶觸摸地圖時添加標記?這裏是代碼:

map.setOnMapClickListener(new OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng point) { 
      map.addMarker(new MarkerOptions().position(point)); 
      // you can get latitude and longitude also from 'point' 
      // and using Geocoder, the address 
     } 
}); 
+0

現在,一旦我得到了點,我怎樣才能將點分成經緯度,以便我可以保存2個變量,然後保存到MySQL? –

+1

我們有'LatLng點'。從那個,我們得到'point.latitude'&'point.longitude' – Nizam

+0

哦,謝謝你的回答,Nizam。 =) –

0

可以有這樣幾個步驟: -

  1. 給一個按鈕,用戶標記他/她的當前位置。
  2. 的OnClick該按鈕,獲取當前位置,並將其保存在您的文件/數據庫等
  3. 使用存儲的位置添加標記在地圖上並通知用戶
+0

nono不是當前位置,因爲用戶可以在任何地方,並且用戶想要添加他/她的房屋位置 –

+0

然後您給他一個選項來在地圖上添加標記,將其屬性設置爲可拖動,並讓用戶拖動標記到他/她想要的位置。現在他完成後,從該位置獲取地理座標並保存。你可以搜索,如何獲得一個地方的地理位置的地方,當標記被拖動在Android谷歌地圖 –

+0

好吧...我只需要嘗試獲得座標,如果得到源代碼更好 –

1

可以使用的地理編碼那。地理編碼是將街道地址或其他位置描述轉換爲(緯度,經度)座標的過程。一旦你獲得了座標,你可以在那裏添加標記。

+0

我會嘗試,我仍然不確定它是否會生效 –

+0

我同意Kat-hat –

+0

如果能夠提供給我一些源代碼,用戶在不知道座標和地址的情況下添加標記(此代碼在用戶添加標記後發生)會更好。 –