2013-06-27 116 views
0

我在Eclipse中使用Api Key v1和Android現在我試圖將它轉換爲v2。但我總感到很容易做到這一點。我已經完成了一些努力,我得到了Map Api key v2,並且還根據例如Google Play Services等更新了mY SDk,並根據該更改了我的清單文件。現在它的時間到了改變了我該圖是我的地圖和標記從JSON Api.Here源代碼是所有關於我的應用 佈局文件將android map v1轉換爲google android map map v2

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".GMapsActivity" > 

    <fragment 
     android:id="@+id/map_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.MapFragment" /> 

    <EditText 
     android:id="@+id/pass" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:width="150dp" 
     android:gravity="center_horizontal" 
     /> 

    <Button 
     android:id="@+id/login" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/pass" 
     android:layout_marginLeft="44dp" 
     android:text="@string/log" /> 

</RelativeLayout> 

清單

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="map3.pkg.pkg" 
     android:versionCode="1" 
     android:versionName="1.0" 
     android:installLocation="auto"> 
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" /> 

    <permission android:name="map2.pkg.pkg.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> 
    <uses-feature android:glEsVersion="0x00020000" android:required="true" /> 
    <uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity 
      android:name="map3.pkg.pkg.GMapsActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyBxjGroO8h6KXAmxEGyTwR-MXqjLnYwU_k" /> 

     <uses-library android:name="com.google.android.maps" /> 
    </application> 
</manifest> 

源文件

本規範Actualy V1和現在我想將它轉換V3

package map3.pkg.pkg; 

import java.util.List; 

//import android.R; 
import android.graphics.drawable.Drawable; 
import android.net.ParseException; 
import android.os.Bundle; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import map2.pkg.pkg.R; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.util.Log; 

public class GMapsActivity extends MapActivity { 

private MapView mapView; 
EditText password; 
Button login; 
TextView vwpass; 

JSONArray jArray; 
String result = null; 
InputStream is = null; 
StringBuilder sb=null; 

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


password=(EditText)findViewById(R.id.pass); 
login=(Button)findViewById(R.id.login); 
mapView = (MapView) findViewById(R.id.map_view);  
     mapView.setBuiltInZoomControls(true); 

     login.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v) 
      { 
       if(password.getText().toString().equals("tsml")) { 
       mapView.setVisibility(View.VISIBLE); 
       login.setVisibility(View.INVISIBLE); 
       password.setVisibility(View.INVISIBLE); 
       } else { 
       Toast.makeText(getBaseContext(), "invalid password - try again", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://221.120.216.52/a/map.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
      }catch(Exception e){ 
       Log.i("log_tag", "Error in http connection"+e.toString()); 
      } 
     //convert response to string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 

      String line="0"; 
      while ((line = reader.readLine()) != null) { 
          sb.append(line + "\n"); 
       } 
       is.close(); 
       result=sb.toString(); 
       }catch(Exception e){ 
        Log.e("log_tag", "Error converting result "+e.toString()); 
       } 
//paring data 
double LAT; 
double LANG; 
String INFO; 
//result="[{\"lat\":\"31.5079616\",\"lang\":\"74.3376768\",\"info\":\" AQ Corolla (LZA 516)\",\"groupID\":\"Lahore\"},{\"lat\":\"31.5260192\",\"lang\":\"74.3768128\",\"info\":\" LC1 Corolla (LEA 6739)\",\"groupID\":\"Lahore\"}]"; 
try{ 
     jArray = new JSONArray(result); 
     JSONObject json_data=null; 
     for(int i=0;i<jArray.length();i++){ 
      json_data = jArray.getJSONObject(i); 
      LAT=json_data.getDouble("lat"); 
      LANG=json_data.getDouble("lang"); 
      INFO=json_data.getString("info"); 


//Overlay code 
List<Overlay> mapOverlays = mapView.getOverlays(); 
       Drawable drawable = this.getResources().getDrawable(R.drawable.icon); 
     CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this); 

     //GeoPoint point = new GeoPoint(latitudeE6, longitudeE6); 
     GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6)); 
     OverlayItem overlayitem = new OverlayItem(point, "Shakarganj", INFO); 
     itemizedOverlay.addOverlay(overlayitem); 
     mapOverlays.add(itemizedOverlay); 

     MapController mapController = mapView.getController(); 

     mapController.animateTo(point); 
     mapController.setZoom(6); 

//Overlay code 

     } 
     } 
     catch(JSONException e1){ 
      Toast.makeText(getBaseContext(), "No Vehicles Found" ,Toast.LENGTH_LONG).show(); 
     } catch (ParseException e1) { 
      e1.printStackTrace(); 
    } 
} 
@Override 
protected boolean isRouteDisplayed() { 
    return false; 
} 
} 

這是剛開點從URL,並將其轉換爲字符串然後解析使用JSON,然後顯示在地圖上.Plz任何一個幫助我,我怎樣才能將此代碼轉換爲顯示API的地圖v3.Plz幫助

回答

3

如果您已經能夠使用谷歌播放服務(地圖API V2)顯示地圖,你已經做了很多工作。

二,請在那個時候看看Google Documentation for the new Android Maps V2 API。它們覆蓋了大多數情況非常好....

第三,您的JSON代碼不受影響。就你而言,API的最大變化是標記管理。

您可以更改您要添加一個標記到地圖

List<Overlay> mapOverlays = mapView.getOverlays(); 
Drawable drawable = this.getResources().getDrawable(R.drawable.icon); 
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this); 

    //GeoPoint point = new GeoPoint(latitudeE6, longitudeE6); 
    GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6)); 
    OverlayItem overlayitem = new OverlayItem(point, "Shakarganj", INFO); 
    itemizedOverlay.addOverlay(overlayitem); 
    mapOverlays.add(itemizedOverlay); 

    MapController mapController = mapView.getController(); 

    mapController.animateTo(point); 
    mapController.setZoom(6); 

有了這個

LatLng markerPosition = new LatLng(LAT ,LANG); 
googleMap.addMarker(new MarkerOptions().position(markerPosition 
         .title("Shakarganj") 
         .snippet(INFO)); 

無需與覆蓋工作了這種原始的代碼。

如果要動畫您使用的animateCamera方法地圖上的標記:

CameraPosition cameraPosition = new CameraPosition.Builder().target(markerPosition); 
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

,而不是使用mapController。