Storing Google Maps Android API V2 marker locations in MySQL

T

theAydinator

Neues Mitglied
0
Schönen guten Abend,

ich versuche vergeblich das folgende Tutorial zu verstehen und umzusetzen,
leider scheitert die App beim Starten.

Link zum Totorial: Storing Google Maps Android API V2 marker locations in MySQL | Knowledge by Experience

Am Ende habe ich sogar den Quelcode heruntergeladen und als "Existing Android Code..." importiert.

Ich bin ein Anfänger und würde mich über Tipps freuen, die mich weiterbringen würden.

Ich danke Euch schonmal!

Mainactivity:
Code:
package in.wptrafficanalyzer.locationmarkermysql;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {

    GoogleMap mGoogleMap;

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

        // Getting reference to SupportMapFragment
        SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Creating GoogleMap from SupportMapFragment
        mGoogleMap = fragment.getMap();
        
        // Enabling MyLocation button for the Google Map
        mGoogleMap.setMyLocationEnabled(true);
        
        // Setting OnClickEvent listener for the GoogleMap
        mGoogleMap.setOnMapClickListener(new OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latlng) {
                addMarker(latlng);
                sendToServer(latlng);
            }
        });
        
        // Starting locations retrieve task
        new RetrieveTask().execute();
        
    }

    // Adding marker on the GoogleMaps
    private void addMarker(LatLng latlng) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latlng);
        markerOptions.title(latlng.latitude + "," + latlng.longitude);
        mGoogleMap.addMarker(markerOptions);
    }

    // Invoking background thread to store the touched location in Remove MySQL server
    private void sendToServer(LatLng latlng) {
        new SaveTask().execute(latlng);
    }

    
    // Background thread to save the location in remove MySQL server
    private class SaveTask extends AsyncTask<LatLng, Void, Void> {
        @Override
        protected Void doInBackground(LatLng... params) {
            String lat = Double.toString(params[0].latitude);
            String lng = Double.toString(params[0].longitude);
            String strUrl = "http://domain.com/app/location_marker_mysql/save.php";                    
            URL url = null;
            try {
                url = new URL(strUrl);

                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                        connection.getOutputStream());

                outputStreamWriter.write("lat=" + lat + "&lng="+lng);                
                outputStreamWriter.flush();
                outputStreamWriter.close();
                
                InputStream iStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new
                InputStreamReader(iStream));
                
                StringBuffer sb = new StringBuffer();
                
                String line = "";
                
                while( (line = reader.readLine()) != null){
                    sb.append(line);
                }

                reader.close();
                iStream.close();
                            

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

    }
    
    // Background task to retrieve locations from remote mysql server
    private class RetrieveTask extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            String strUrl = "http://domain.com/app/location_marker_mysql/retrieve.php";                
            URL url = null;
            StringBuffer sb = new StringBuffer();
            try {
                url = new URL(strUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream iStream = connection.getInputStream();                
                BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));            
                String line = "";                
                while( (line = reader.readLine()) != null){
                    sb.append(line);
                }

                reader.close();
                iStream.close();                            

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }        
            return sb.toString();
        }
        
        @Override
        protected void onPostExecute(String result) {            
            super.onPostExecute(result);
            new ParserTask().execute(result);
        }
        
    }
    
    // Background thread to parse the JSON data retrieved from MySQL server
    private class ParserTask extends AsyncTask<String, Void, List<HashMap<String, String>>>{
        @Override
        protected List<HashMap<String,String>> doInBackground(String... params) {
            MarkerJSONParser markerParser = new MarkerJSONParser();
            JSONObject json = null;
            try {
                json = new JSONObject(params[0]);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            List<HashMap<String, String>> markersList = markerParser.parse(json);
            return markersList;
        }
        
        @Override
        protected void onPostExecute(List<HashMap<String, String>> result) {
            for(int i=0; i<result.size();i++){
                HashMap<String, String> marker = result.get(i);
                LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")), Double.parseDouble(marker.get("lng")));
                addMarker(latlng);
            }
        }
    }

    @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;
    }
}
AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.locationmarkermysql"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <!-- Protect the map component of the application using application signature -->
    <permission 
        android:name="in.wptrafficanalyzer.locationmarkermysql.permission.MAPS_RECEIVE" 
        android:protectionLevel="signature" />

    <!--  Allows to receive map -->
    <uses-permission android:name="in.wptrafficanalyzer.locationmarkermysql.permission.MAPS_RECEIVE" />

    <!-- Used by the Google Maps Android API V2 to download map tiles from Google Maps servers -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Allows the Google Maps Android API V2 to cache map tile data in the device's external storage area -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Allows the Google Maps Android API V2 to use WiFi or mobile cell data (or both) to determine the device's location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <!-- Allows the Google Maps Android API V2 to use the Global Positioning System (GPS) 
    to determine the device's location to within a very small area -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Allows to contact Google Serves -->
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    
     <!-- Google Maps Android API V2 requires OpenGL ES version 2 -->
    <uses-feature 
        android:glEsVersion="0x00020000"
        android:required="true" />    
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.wptrafficanalyzer.locationmarkermysql.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- Specifies the Android API Key, which is obtained from Google API Console -->
        <meta-data 
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAaxdt5QHJ0VQCOH1uOfRsPmf5RH7yWcZs" />
        
    </application>

</manifest>
Im Eclipse bekomme ich beim Debuggen keine Fehlermeldung.

Unter MainActivity.java habe ich nur den Link zu den PHP-Dateien geändert
und unter AndroidManifest.xml den Google API Key hinzugefügt.
Alles andere gleich geblieben.

Google Maps Services wurden auch eingebunden.

DANKE
Aydin

Der ursprüngliche Beitrag von 03:13 Uhr wurde um 03:21 Uhr ergänzt:

Zum Testen habe ich in die res/layout/activity_main.xml statt den Fragment einen TextView hinzugefügt und unter MainActivity.java die Zeilen in der Methode onCreate() ab

Code:
setContentView(R.layout.activity_main);
auskommentiert.

Das läuft einwandfrei.

Der ursprüngliche Beitrag von 03:21 Uhr wurde um 03:27 Uhr ergänzt:

Nun habe ich den Fragment in das Layout zurückgeholt und wollte die MainActivity allein das Layout aufrufen ohne den weiteren Einstellungen.

Code:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}
Und leider stürzt die Anwendung ab.

Der ursprüngliche Beitrag von 03:27 Uhr wurde um 04:49 Uhr ergänzt:

Ich befürchte das Problem liegt an demm KEYSTORE.

Ich habe, wie auf dieser Seite Android SDK: Working with Google Maps - Application Setup - Tuts+ Code Tutorial (2. Get a Google Maps API Key), den
SHA-1 Code generiert und es während dem Generieren von API KEY hinzugefügt. Anschließend den API-KEY habe ich in die ManifestDatei hinzugefügt.
Nun wahrscheinlich die entscheidenede Frage:
Welchen keystore beim Exportieren (zum .apk) muss ich auswählen und wo befindet der sich?


Der ursprüngliche Beitrag von 04:49 Uhr wurde um 05:10 Uhr ergänzt:

Ich habe nun meine Einstellungen während dem Export wie folgt angepasst:


  • Keystore name: "debug.keystore"
  • Keystore password: "android"
  • Key alias: "androiddebugkey"
  • Key password: "android"
Quelle: Signing Your Applications | Android Developers


Klappt leider weiterhin nicht.

Der ursprüngliche Beitrag von 05:10 Uhr wurde um 05:52 Uhr ergänzt:

Ich würde mich SEHR SEHR freuen,
wenn jemand mir bitte auf die Sprünge hilft!

Der ursprüngliche Beitrag von 05:52 Uhr wurde um 06:37 Uhr ergänzt:

Habe mein Manifest überarbeitet und es funktioniert :D

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.locationmarkermysql"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />
    
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <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/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        
        <activity
            android:name="in.wptrafficanalyzer.locationmarkermysql.MainActivity"
            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="API_KEY" />
        
    </application>

</manifest>
 

Ähnliche Themen

2
Antworten
6
Aufrufe
295
24559
2
D
Antworten
23
Aufrufe
2.665
Data2006
D
Zurück
Oben Unten