2013-04-06 32 views
4

嗨iam在我的應用程序非常做一個應用程序有一個地圖頁面,所以我實施谷歌地圖V2和我有一個問題,當我滾動地圖我想要得到地圖的中心點,如果我滾動地圖我離開它然後它得到了地圖的中心點,所以任何人都可以建議解決這個問題,它可能會有所幫助先謝謝您......如何獲取中心點,當我在谷歌地圖v2在android中滾動地圖?

注意:我已經使用a谷歌地圖V2,所以請發佈相關的。

public class Mapview extends FragmentActivity implements OnMapClickListener, OnCameraChangeListener{ 

final int RQS_GooglePlayServices = 1; 
private GoogleMap myMap; 
Location myLocation; 
TextView tvLocInfo; 
GPSTracker gps; 
public double Latitude,Longitude; 
String Datetime, addr,RegUID; 
public String lat,longd; 

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

    tvLocInfo = (TextView)findViewById(R.id.locinfo); 

    FragmentManager myFragmentManager = getSupportFragmentManager(); 
    SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map); 

    myMap = mySupportMapFragment.getMap(); 

    myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    myMap.setMyLocationEnabled(true); 
    myMap.setOnMapClickListener(this); 
    Projection P = myMap.getProjection(); 
    Log.e("lat", String.valueOf(Latitude)); 
    Log.e("lat", String.valueOf(Longitude)); 
    Button mDone = (Button) findViewById(R.id.button1);   
    mDone.setOnClickListener(new View.OnClickListener() { 
     @SuppressLint("SimpleDateFormat") 
     public void onClick(View v) { 

      //Toast.makeText(getBaseContext(), "latitude"+lat+""+"longitude"+longd , Toast.LENGTH_LONG).show(); 
      String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());     
      Datetime = timeStamp.toString(); 
      Log.e("t", Datetime);    
      RegUID = Utils.RegisterUserId; 
      Log.e("t", RegUID); 
      final ProgressDialog dialog = ProgressDialog.show(Mapview.this, "", Utils.Loading, true); 
      dialog.show(); 
      Handler handler = new Handler(); 
      handler.postDelayed(new Runnable(){ 
       @SuppressWarnings("unused") 
       public void run() 
       { 
        try 
        { 
         String EMPLOYEE_SERVICE_URI = Utils.Request+"UserID="+RegUID+"&Location="+URLEncoder.encode(addr,"UTF-8")+"&Latitude="+lat+"&Longitude="+longd+"&RequestDate="+URLEncoder.encode(Datetime,"UTF-8"); 
         Log.e(EMPLOYEE_SERVICE_URI, EMPLOYEE_SERVICE_URI); 
         JSONObject JObject = Utils.getResult(EMPLOYEE_SERVICE_URI); 
         //Toast.makeText(Mapview.this,JObject.toString(), Toast.LENGTH_LONG).show(); 
         if(JObject!=null) 
         {  

          if(JObject.getBoolean("Valid")) 
          { 

           AlertDialog alertDialog = new AlertDialog.Builder(Mapview.this).create(); 
           Utils.callAlert(JObject.getString("Message"), alertDialog); 

          } 
          else 
          { 
           AlertDialog alertDialog = new AlertDialog.Builder(Mapview.this).create(); 
           Utils.callAlert(JObject.getString("Message"), alertDialog); 
          } 

         } 
         else 
         { 
          AlertDialog alertDialog = new AlertDialog.Builder(Mapview.this).create(); 
          Utils.callAlert(JObject.getString("Message"), alertDialog); 
         } 
        } 
        catch (Exception e) 
        { 
         //Toast.makeText(Mapview.this,e.toString(), Toast.LENGTH_LONG).show(); 
         Log.e("Exception", e.toString()); 
        } 
        dialog.dismiss(); 
       }//run() ends 
      }, 5000);    


     } 
    }); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 

    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 

    if (resultCode == ConnectionResult.SUCCESS){ 
     Toast.makeText(getApplicationContext(), 
       "isGooglePlayServicesAvailable SUCCESS", 
       Toast.LENGTH_LONG).show(); 
    }else{ 
     GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices); 
    } 

} 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
    Log.i("test","onScroll"); 
    return false; 
} 
@Override 
public void onMapClick(LatLng point) { 
    Log.e("lat", String.valueOf(point.latitude)); 
    Log.e("lat", String.valueOf(point.longitude)); 
    Latitude =point.latitude; 
    Longitude = point.longitude; 
    lat = String.valueOf(point.latitude); 
    longd = String.valueOf(point.longitude);   
    myMap.moveCamera(CameraUpdateFactory.newLatLng(point)); 
    gps = new GPSTracker(Mapview.this); 
    // check if GPS enabled  
    if(gps.canGetLocation()) 
    { 

     Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); 
     try { 
      List<Address> addresses = geoCoder.getFromLocation(Latitude, Longitude, 1); 
      if(addresses != null) { 
       Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder(""); 
       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
       } 
       addr = new String(strReturnedAddress); 
       tvLocInfo.setText(addr.toString()); 

       //Toast.makeText(getApplicationContext(), addr.toString(),Toast.LENGTH_LONG).show(); 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), "Gps location address is unavailable please try again later", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 

      e.printStackTrace(); 
     } 
    } 
} 

@Override 
public void onCameraChange(CameraPosition arg0) { 
    // TODO Auto-generated method stub 

} 

} 

回答

7

您可以使用: -

myMap.setOnCameraChangedListener(this); 

而在聽衆: -

onCameraChange(CameraPosition position) { 
    LatLng target = position.target; 
    // do what you need with the position here 
} 

我不知道有關索引出界。但設置默認的地圖位置,你可以使用類似如下: -

private void setDefaultMapPosition(LatLng latLng) { 

    CameraPosition camPos = 
      new CameraPosition.Builder().target(latLng) 
      .zoom(A_DEFAULT_MAP_ZOOM) 
      .bearing(0) 
      .tilt(0) 
      .build(); 

    myMap.moveCamera(
      CameraUpdateFactory.newCameraPosition(camPos)); 

    } 
+0

非常感謝您的回答.......我有一個問題,當我打開地圖它顯示'LatLang(0.0 ,0.0)'和強制關閉應用程序錯誤索引出債券... – androidgeek 2013-04-06 08:41:49

+0

@Ryan,非常感謝你,幫助我很多。 – 2013-09-27 13:38:27